
[手動(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
新聞熱點(diǎn)
疑難解答
圖片精選