程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 用C#獲取文件類型信息

用C#獲取文件類型信息

編輯:關於C語言

[手動查看]
資源管理器 -> 工具 -> 文件夾選項 -> 文件類型

[C/C++]
#include <Windows.h>
#include <shellapi.h> //SHFILEINFO結構和SHGetFileInfo函數所在
//#include <stdio.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    SHFILEINFO shfi;
    memset(&shfi,0,sizeof(shfi));
    SHGetFileInfo("C:\\Windows\\notepad.exe", //文件路徑
        FILE_ATTRIBUTE_NORMAL,                //文件屬性
        &shfi,                                //信息結構
        sizeof(shfi),
        SHGFI_TYPENAME);                      //獲取標識
  
    char *msg = shfi.szTypeName;
    
    //在DOS窗口輸出
    //printf("%s\n",msg);
    //getchar();
    
    LPCTSTR lpcstr = (LPCTSTR)msg; //char*轉換LPCTSTR
    MessageBox(NULL, lpcstr, "文件類型", MB_OK);
    return 0;
}



[C#] - mode 1
using Microsoft.Win32;

//通過查詢注冊表來獲取文件類型名稱,但此方法具有致命缺陷!
string ext = ".mp3";
string desc = (string)Registry.ClassesRoot.OpenSubKey(ext).GetValue(null);
string typeinfo = (string)Registry.ClassesRoot.OpenSubKey(desc).GetValue(null);



[C#] - mode 2
using System.Runtime.InteropServices;

//在shell32.dll導入函數SHGetFileInfo
[DllImport("shell32.dll", EntryPoint="SHGetFileInfo")]
public static extern int GetFileInfo(string pszPath, int dwFileAttributes,
    ref FileInfomation psfi, int cbFileInfo,int uFlags);

//定義SHFILEINFO結構(名字隨便起,這裡用FileInfomation)
[StructLayout(LayoutKind.Sequential)]
public struct FileInfomation
{
    public IntPtr hIcon;
    public int iIcon;
    public int dwAttributes;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)]
    public string szDisplayName;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=80)]
    public string szTypeName;
}

//定義文件屬性標識
public enum FileAttributeFlags : int
{
    FILE_ATTRIBUTE_READONLY            =0x00000001,
    FILE_ATTRIBUTE_HIDDEN              =0x00000002,
    FILE_ATTRIBUTE_SYSTEM              =0x00000004,
    FILE_ATTRIBUTE_DIRECTORY           =0x00000010,
    FILE_ATTRIBUTE_ARCHIVE             =0x00000020,
    FILE_ATTRIBUTE_DEVICE              =0x00000040,
    FILE_ATTRIBUTE_NORMAL              =0x00000080,
    FILE_ATTRIBUTE_TEMPORARY           =0x00000100,
    FILE_ATTRIBUTE_SPARSE_FILE         =0x00000200,
    FILE_ATTRIBUTE_REPARSE_POINT       =0x00000400,
    FILE_ATTRIBUTE_COMPRESSED          =0x00000800,
    FILE_ATTRIBUTE_OFFLINE             =0x00001000,
    FILE_ATTRIBUTE_NOT_CONTENT_INDEXED =0x00002000,
    FILE_ATTRIBUTE_ENCRYPTED           =0x00004000
}

//定義獲取資源標識
public enum GetFileInfoFlags : int
{
    SHGFI_ICON              =0x000000100,     // get icon
    SHGFI_DISPLAYNAME       =0x000000200,     // get display name
    SHGFI_TYPENAME          =0x000000400,     // get type name
    SHGFI_ATTRIBUTES        =0x000000800,     // get attributes
    SHGFI_ICONLOCATION      =0x000001000,     // get icon location
    SHGFI_EXETYPE           =0x000002000,     // return exe type
    SHGFI_SYSICONINDEX      =0x000004000,     // get system icon index
    SHGFI_LINKOVERLAY       =0x000008000,     // put a link overlay on icon
    SHGFI_SELECTED          =0x000010000,     // show icon in selected state
    SHGFI_ATTR_SPECIFIED    =0x000020000,     // get only specifIEd attributes
    SHGFI_LARGEICON         =0x000000000,     // get large icon
    SHGFI_SMALLICON         =0x000000001,     // get small icon
    SHGFI_OPENICON          =0x000000002,     // get open icon
    SHGFI_SHELLICONSIZE     =0x000000004,     // get shell size icon
    SHGFI_PIDL              =0x000000008,     // pszPath is a pidl
    SHGFI_USEFILEATTRIBUTES =0x000000010,     // use passed dwFileAttribute
    SHGFI_ADDOVERLAYS       =0x000000020,     // apply the appropriate overlays
    SHGFI_OVERLAYINDEX      =0x000000040      // Get the index of the overlay
}


private string GetTypeName(string fileName)
{
    FileInfomation fileInfo = new FileInfomation();  //初始化FileInfomation結構
    
    //調用GetFileInfo函數,最後一個參數說明獲取的是文件類型(SHGFI_TYPENAME)
    int res = GetFileInfo(fileName, (int)FileAttributeFlags.FILE_ATTRIBUTE_NORMAL,
        ref fileInfo, Marshal.SizeOf(fileInfo), (int)GetFileInfoFlags.SHGFI_TYPENAME);

    return fileInfo.szTypeName;
}

private void Form1_Load(object sender, System.EventArgs e)
{
    string fileName = @"C:\Windows\notepad.exe";  //定義文件路徑
    string fileTypeName = GetTypeName(fileName);

    MessageBox.Show(fileTypeName);
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved