C#中通過API呼叫獲取檔案圖示
在C#中要獲取各種檔案的圖示,可以通過API呼叫來完成。主要的函式是SHGetFileInfo.在C#中操作的方式如下 :
第一步:準備SHGetFileInfo需要的各種引數
第一步:準備SHGetFileInfo需要的各種引數
1.FileInfoStruct
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->[StructLayout( LayoutKind.Sequential)]
public struct FileInfoStruct
...{
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;
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->[StructLayout( LayoutKind.Sequential)]
public struct FileInfoStruct
...{
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;
}
2.FileInfoFlags
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->public enum FileInfoFlags : 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
} 3.FileAttributeFlags(這個.net中好像已經有了)
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
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->public enum FileInfoFlags : 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
} 3.FileAttributeFlags(這個.net中好像已經有了)
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
}
然後 開始準備呼叫SHELL API了 :
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->[DllImport("shell32.dll " , EntryPoint ="SHGetFileInfo")]
public static extern int GetFileInfo( string pszPath, int dwFileAttributes,
ref FileInfoStruct psfi, int cbFileInfo, int uFlags);
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->[DllImport("shell32.dll " , EntryPoint ="SHGetFileInfo")]
public static extern int GetFileInfo( string pszPath, int dwFileAttributes,
ref FileInfoStruct psfi, int cbFileInfo, int uFlags);
好了現在一切都準備好了 我們現在通過下面的兩個方法來獲取圖示
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->public static Icon GetLargeIcon(string pFilePath)
...{
FileInfoStruct _info = new FileInfoStruct();
Win32API.GetFileInfo(pFilePath, 0 , ref _info, Marshal.SizeOf(_info),
( int )(FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_LARGEICON));
try
... catch
...{
return null ;
}
}
public static Icon GetSmallIcon(string pFilePath)
...{
FileInfoStruct _info = new FileInfoStruct();
Win32API.GetFileInfo(pFilePath, 0 , ref _info, Marshal.SizeOf(_info),
( int )(FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_SMALLICON));
try
...
catch
...{
return null ;
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->public static Icon GetLargeIcon(string pFilePath)
...{
FileInfoStruct _info = new FileInfoStruct();
Win32API.GetFileInfo(pFilePath, 0 , ref _info, Marshal.SizeOf(_info),
( int )(FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_LARGEICON));
try
... catch
...{
return null ;
}
}
public static Icon GetSmallIcon(string pFilePath)
...{
FileInfoStruct _info = new FileInfoStruct();
Win32API.GetFileInfo(pFilePath, 0 , ref _info, Marshal.SizeOf(_info),
( int )(FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_SMALLICON));
try
...
catch
...{
return null ;
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/14766526/viewspace-562963/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 通過web url獲取檔案資訊Web
- 通過反射獲取上傳檔案方法引數中的檔名反射
- vue+axio通過獲取dom元素上傳檔案Vue
- C#通過反射獲取類中的方法和引數個數,反射呼叫方法帶引數C#反射
- 記一次通過c#運用GraphQL呼叫Github apiC#GithubAPI
- c#獲取word檔案頁數、字數C#
- JAVA通過URL連結獲取視訊檔案資訊(無需下載檔案)Java
- zabbix api 獲取圖形API
- 通過可寫檔案獲取 Linux root 許可權的 5 種方法Linux
- 如何呼叫API獲取你想要的資料API
- 【API】隨機獲取圖片API隨機
- Springboot 獲取jar包中的檔案Spring BootJAR
- 實現通過COM元件方式實現java呼叫C#寫的DLL檔案的完整demo元件JavaC#
- 通過用shellcode獲取shell
- C#讀取Xml檔案C#XML
- 聊天平臺原始碼,通過MediaStore獲取縮圖模糊原始碼AST
- Java中的獲取檔案的物理絕對路徑,和讀取檔案Java
- 在Spring boot中通過ApplicationContext獲取bean失敗Spring BootAPPContextBean
- Grails通過sessionId獲取session物件AISession物件
- JavaScript 通過class獲取元素物件JavaScript物件
- 通過url動態獲取圖片大小方法總結
- Python呼叫ansible API系列(一)獲取資產資訊PythonAPI
- 使用Python呼叫API介面獲取淘寶商品資料PythonAPI
- C#讀取文字檔案和寫文字檔案C#
- laravel 使用 axios 通過 put 上傳檔案獲取不到資料的解決辦法LaraveliOS
- Java根據地理位置獲取經緯度(呼叫百度地圖API)Java地圖API
- C#讀取Json配置檔案C#JSON
- 獲取拖拽和剪貼簿中的檔案
- SpringBoot 中獲取專案的路徑和檔案流Spring Boot
- SpringBoot專案中獲取配置檔案的配置資訊Spring Boot
- Maui 讀取外部檔案顯示到Blazor中UIBlazor
- linux檔案相關命令 透過檔案獲取父資料夾名稱Linux
- google books api 獲取圖書資訊GoAPI
- Linux漏洞挖掘:08---系統呼叫劫持之(通過IDT中斷向量表獲取sys_call_table系統呼叫表)Linux
- ajax上傳檔案,spring mvc獲取檔案並處理,通過頁面按鈕傳送url,由後臺控制檔案下載SpringMVC
- 遞迴獲取檔案列表遞迴
- ios端app讀取iphone檔案(通過itunes實現)iOSAPPiPhone
- 呼叫百度api透過經緯度獲取實際地理位置資訊API
- c# 呼叫微吼直播APIC#API