国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

用C#獲取文件類型信息

2024-07-21 02:26:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

[手動(dòng)查看]
資源管理器 -> 工具 -> 文件夾選項(xiàng) -> 文件類型

[c/c++]
#include <windows.h>
#include <shellapi.h> //shfileinfo結(jié)構(gòu)和shgetfileinfo函數(shù)所在
//#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,                                //信息結(jié)構(gòu)
        sizeof(shfi),
        shgfi_typename);                      //獲取標(biāo)識(shí)
  
    char *msg = shfi.sztypename;
    
    //在dos窗口輸出
    //printf("%s/n",msg);
    //getchar();
    
    lpctstr lpcstr = (lpctstr)msg; //char*轉(zhuǎn)換lpctstr
    messagebox(null, lpcstr, "文件類型", mb_ok);
    return 0;
}



[c#] - mode 1
using microsoft.win32;

//通過(guò)查詢注冊(cè)表來(lái)獲取文件類型名稱,但此方法具有致命缺陷!
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導(dǎo)入函數(shù)shgetfileinfo
[dllimport("shell32.dll", entrypoint="shgetfileinfo")]
public static extern int getfileinfo(string pszpath, int dwfileattributes,
    ref fileinfomation psfi, int cbfileinfo,int uflags);

//定義shfileinfo結(jié)構(gòu)(名字隨便起,這里用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;
}

//定義文件屬性標(biāo)識(shí)
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
}

//定義獲取資源標(biāo)識(shí)
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結(jié)構(gòu)
    
    //調(diào)用getfileinfo函數(shù),最后一個(gè)參數(shù)說(shuō)明獲取的是文件類型(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);
}



后記:搞了一個(gè)深夜,一頭霧水~~~




[示例源代碼]
點(diǎn)擊下載該文件

此主題相關(guān)圖片如下:

 

商業(yè)源碼熱門(mén)下載www.html.org.cn

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 盐亭县| 保山市| 延长县| 景洪市| 会东县| 图们市| 宝清县| 重庆市| 化德县| 桃园市| 镇江市| 通榆县| 绥阳县| 郯城县| 延边| 大城县| 泰州市| 轮台县| 商河县| 平罗县| 达拉特旗| 邛崃市| 博湖县| 华阴市| 和林格尔县| 教育| 高邑县| 阜平县| 长春市| 法库县| 普兰县| 丰原市| 蒙城县| 大化| 河南省| 丰宁| 房山区| 乌苏市| 图们市| 宁蒗| 会宁县|