[Manual view]
Resource Manager-> Tools-> Folder Options-> file types
[C + +]
#include <windows.h>
#include <shellapi.h>//shfileinfo structure and Shgetfileinfo functions
#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 path
File_attribute_normal,//file properties
&SHFI,//Information structure
sizeof (SHFI),
Shgfi_typename); Get identity
char *msg = shfi.sztypename;
Output in DOS window
printf ("%s\n", msg);
GetChar ();
LPCTSTR LPCSTR = (LPCTSTR) msg; char* Conversion LPCTSTR
MessageBox (NULL, LPCSTR, "file type", MB_OK);
return 0;
}
[C #]-mode 1
Using Microsoft.Win32;
Get the file type name by querying the registry, but this method has a fatal flaw!
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;
Import functions in Shell32.dll Shgetfileinfo
[DllImport ("Shell32.dll", entrypoint= "Shgetfileinfo")]
public static extern int GetFileInfo (string pszpath, int dwfileattributes,
Ref fileinfomation PSFI, int cbfileinfo,int uflags);
Define SHFILEINFO structure (name casually, here with 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;
}
Define file attribute identification
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
}
Define Get resource Identification
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 (); Initialize fileinfomation structure
Call the GetFileInfo function, and the last parameter indicates that the file type was obtained (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"; Define file path
String filetypename = Gettypename (fileName);
MessageBox.Show (Filetypename);
}
PostScript: Engaged in a late night, confused ~ ~ ~ ~
[Sample source Code]
Click to download the file
The relevant pictures of this topic are as follows: