日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#中读取“已注册的文件类型”的图标及读取指定文件图标的方法 (转)

發布時間:2023/12/18 C# 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中读取“已注册的文件类型”的图标及读取指定文件图标的方法 (转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
using?System;
using?System.IO;
using?System.Drawing;
using?Microsoft.Win32;
using?System.Runtime.InteropServices;


namespace?GetIconDemo
{
??
///?<summary>
??
///?提供從操作系統讀取圖標的方法
??
///?</summary>
??public?class?GetSystemIcon
??{
????
///?<summary>
????
///?依據文件名讀取圖標,若指定文件不存在,則返回空值。
????
///?</summary>
????
///?<param?name="fileName"></param>
????
///?<returns></returns>
????public?static?Icon?GetIconByFileName(string?fileName)
????{
????????
if?(fileName?==?null?||?fileName.Equals(string.Empty))?return?null;
????????
if?(!File.Exists(fileName))?return?null;

????????SHFILEINFO?shinfo?
=?new?SHFILEINFO();
????????
//Use?this?to?get?the?small?Icon
????????Win32.SHGetFileInfo(fileName,?0,?ref?shinfo,?(uint)Marshal.SizeOf(shinfo),?Win32.SHGFI_ICON?|?Win32.SHGFI_SMALLICON);
????????
//The?icon?is?returned?in?the?hIcon?member?of?the?shinfo?struct
????????System.Drawing.Icon?myIcon?=?System.Drawing.Icon.FromHandle(shinfo.hIcon);
????????
return?myIcon;
????}

????
///?<summary>
????
///?給出文件擴展名(.*),返回相應圖標
????
///?若不以"."開頭則返回文件夾的圖標。
????
///?</summary>
????
///?<param?name="fileType"></param>
????
///?<param?name="isLarge"></param>
????
///?<returns></returns>
????public?static?Icon?GetIconByFileType(string?fileType,?bool?isLarge)
????{
????????
if?(fileType?==?null?||?fileType.Equals(string.Empty))?return?null;

????????RegistryKey?regVersion?
=?null;
????????
string?regFileType?=?null;
????????
string?regIconString?=?null;
????????
string?systemDirectory?=?Environment.SystemDirectory?+?"\\";

????????
if?(fileType[0]?==?'.')
????????{
??????????
//讀系統注冊表中文件類型信息
??????????regVersion?=?Registry.ClassesRoot.OpenSubKey(fileType,?true);
??????????
if?(regVersion?!=?null)
??????????{
????????????regFileType?
=?regVersion.GetValue("")?as?string;
????????????regVersion.Close();
????????????regVersion?
=?Registry.ClassesRoot.OpenSubKey(regFileType?+?@"\DefaultIcon",?true);
????????????
if?(regVersion?!=?null)
????????????{
????????????????regIconString?
=?regVersion.GetValue("")?as?string;
????????????????regVersion.Close();
????????????}
??????????}
??????????
if?(regIconString?==?null)
??????????{
????????????
//沒有讀取到文件類型注冊信息,指定為未知文件類型的圖標
????????????regIconString?=?systemDirectory?+?"shell32.dll,0";
??????????}
????????}
????????
else
????????{
??????????
//直接指定為文件夾圖標
??????????regIconString?=?systemDirectory?+?"shell32.dll,3";
????????}
????????
string[]?fileIcon?=?regIconString.Split(new?char[]?{?','?});
????????
if?(fileIcon.Length?!=?2)
????????{
??????????
//系統注冊表中注冊的標圖不能直接提取,則返回可執行文件的通用圖標
??????????fileIcon?=?new?string[]?{?systemDirectory?+?"shell32.dll",?"2"?};
????????}
????????Icon?resultIcon?
=?null;
????????
try
????????{
??????????
//調用API方法讀取圖標
??????????int[]?phiconLarge?=?new?int[1];
??????????
int[]?phiconSmall?=?new?int[1];
??????????
uint?count?=?Win32.ExtractIconEx(fileIcon[0],?Int32.Parse(fileIcon[1]),?phiconLarge,?phiconSmall,?1);
??????????IntPtr?IconHnd?
=?new?IntPtr(isLarge???phiconLarge[0]?:?phiconSmall[0]);
??????????resultIcon?
=?Icon.FromHandle(IconHnd);
????????}
????????
catch?{?}
????????
return?resultIcon;
????}
??}



??[StructLayout(LayoutKind.Sequential)]
??
public?struct?SHFILEINFO
??{
????
public?IntPtr?hIcon;
????
public?IntPtr?iIcon;
????
public?uint?dwAttributes;
????[MarshalAs(UnmanagedType.ByValTStr,?SizeConst?
=?260)]
????
public?string?szDisplayName;
????[MarshalAs(UnmanagedType.ByValTStr,?SizeConst?
=?80)]
????
public?string?szTypeName;
??};

??
///?<summary>
??
///?定義調用的API方法
??
///?</summary>
??class?Win32
??{
????
public?const?uint?SHGFI_ICON?=?0x100;
????
public?const?uint?SHGFI_LARGEICON?=?0x0;?//?'Large?icon
????public?const?uint?SHGFI_SMALLICON?=?0x1;?//?'Small?icon

????[DllImport(
"shell32.dll")]
????
public?static?extern?IntPtr?SHGetFileInfo(string?pszPath,?uint?dwFileAttributes,?ref?SHFILEINFO?psfi,?uint?cbSizeFileInfo,?uint?uFlags);
????[DllImport(
"shell32.dll")]
????
public?static?extern?uint?ExtractIconEx(string?lpszFile,?int?nIconIndex,?int[]?phiconLarge,?int[]?phiconSmall,?uint?nIcons);

??}
}

實例
File Explorer.rar

轉載于:https://www.cnblogs.com/hun_dan/archive/2009/04/26/1443696.html

總結

以上是生活随笔為你收集整理的C#中读取“已注册的文件类型”的图标及读取指定文件图标的方法 (转)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。