[手动查看]
资源管理器 -> 工具 -> 文件夹选项 -> 文件类型
[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);
}
后记:搞了一个深夜,一头雾水~~~
[示例源代码]
点击下载该文件
此主题相关图片如下:
商业源码热门下载www.html.org.cn
新闻热点
疑难解答