C#获取系统图标。

fanbingyuan 2010-12-31 05:03:31
我用ShGetFileInfo 能得到文件的图标,但是像文件夹,磁盘。我的电脑 控制面板之类的图标不知道该如何取得、求大神帮忙,在线等。。。。
新手。研究了一天的ShGetFileInfo API 了
...全文
214 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yzg100 2011-01-01
  • 打赏
  • 举报
回复
直接给文件夹路径就行了.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
if (openFile.ShowDialog() == DialogResult.OK)
{
string filePath = System.IO.Path.GetDirectoryName(openFile.FileName);
this.pictureBox1.Image = GetLargeIcon(filePath).ToBitmap();
this.pictureBox2.Image = GetSmallIcon(filePath).ToBitmap();
}
}

[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;
}

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
}

//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
//}

[DllImport("shell32.dll ", EntryPoint = "SHGetFileInfo")]
public static extern int GetFileInfo(string pszPath, int dwFileAttributes,
ref FileInfoStruct psfi, int cbFileInfo, int uFlags);

public static Icon GetLargeIcon(string pFilePath)
{
FileInfoStruct _info = new FileInfoStruct();
GetFileInfo(pFilePath, 0, ref _info, Marshal.SizeOf(_info),
(int)(FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_LARGEICON));
try
{
return Icon.FromHandle(_info.hIcon);
}
catch
{
return null;
}
}
public static Icon GetSmallIcon(string pFilePath)
{
FileInfoStruct _info = new FileInfoStruct();
GetFileInfo(pFilePath, 0, ref _info, Marshal.SizeOf(_info),
(int)(FileInfoFlags.SHGFI_ICON | FileInfoFlags.SHGFI_SMALLICON));
try
{
return Icon.FromHandle(_info.hIcon);
}
catch
{
return null;
}
}
}
}
fanbingyuan 2011-01-01
  • 打赏
  • 举报
回复
新年快乐,但是我们不放假,还得继续、我就是不知道取像文件夹,硬盘。光驱这类的图标的时候该如何赋值啊。UPUP
wuyq11 2010-12-31
  • 打赏
  • 举报
回复
public static Icon GetFileIcon(string fileName, IconSize iconSize)
{
Icon icon = null;
try
{
SHFILEINFO psfi = new SHFILEINFO();
Win32.SHGetFileInfo(fileName, 0, ref psfi, (uint) Marshal.SizeOf(psfi), (uint) (0x100 | ((iconSize == IconSize.Small) ? 1 : 0)));
icon = Icon.FromHandle(psfi.hIcon);
}
catch
{
}
return icon;
}

public static string GetTypeName(string fileName)
{
SHFILEINFO psfi = new SHFILEINFO();
Win32.SHGetFileInfo(fileName, 0x80, ref psfi, (uint) Marshal.SizeOf(psfi), 0x400);
return psfi.szTypeName;
}

[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;
}

private class Win32
{
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0;
public const uint SHGFI_SMALLICON = 1;

[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref IconExtractor.SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
}
dylike 2010-12-31
  • 打赏
  • 举报
回复
My.Computer.Registry.GetValue("HKEY_CLASSES_ROOT\Directory\DefaultIcon", "", "")
ppyyhh 2010-12-31
  • 打赏
  • 举报
回复
呵呵,下面是完整的API构造和调用方法
using System;
using System.Runtime.InteropServices;

namespace MediaClient
{
public class Win32
{
[DllImport("shell32.dll", EntryPoint = "ExtractIcon")]
public static extern int ExtractIcon(IntPtr hInst,string lpFileName,int nIndex);

[DllImport("shell32.dll", EntryPoint = "SHGetFileInfo")]
public static extern IntPtr SHGetFileInfo(string pszPath,uint dwFileAttribute,ref SHFILEINFO psfi,uint cbSizeFileInfo,uint Flags);

[DllImport("User32.dll", EntryPoint = "DestroyIcon")]
public static extern int DestroyIcon(IntPtr hIcon);

[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>
/// 获得指定路径下面的所有文件及文件夹
/// </summary>
/// <param name="path">路径</param>
/// <returns></returns>
public ArrayList GetListViewItem(string path,ImageList imglist)
{
Win32.SHFILEINFO shfi = new Win32.SHFILEINFO();
try
{
string[] dirs = Directory.GetDirectories(path);
string[] files = Directory.GetFiles(path);
ArrayList itemarr = new ArrayList();

for (int i = 0; i < dirs.Length; i++)
{
string[] info = new string[4];
DirectoryInfo dir = new DirectoryInfo(dirs[i]);
//获得图标
Win32.SHGetFileInfo(dirs[i],
(uint)0x80,
ref shfi,
(uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
(uint)(0x100 | 0x400)); //取得Icon和TypeName
//添加图标
imglist.Images.Add(dir.Name, (Icon)Icon.FromHandle(shfi.hIcon).Clone());
info[0] = dir.Name;
info[1] = "";
info[2] = "文件夹";
info[3] = dir.LastWriteTime.ToString();
ListViewItem item = new ListViewItem(info,dir.Name);
itemarr.Add(item);
//销毁图标
Win32.DestroyIcon(shfi.hIcon);
}
for (int i = 0; i < files.Length; i++)
{
string[] info = new string[4];
FileInfo fi = new FileInfo(files[i]);
//获得图标
Win32.SHGetFileInfo(files[i],
(uint)0x80,
ref shfi,
(uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
(uint)(0x100 | 0x400)); //取得Icon和TypeName
//添加图标
imglist.Images.Add(fi.Name,(Icon)Icon.FromHandle(shfi.hIcon).Clone());
info[0] = fi.Name;
info[1] = fi.Length.ToString();
info[2] = fi.Extension.ToString();
info[3] = fi.LastWriteTime.ToString();
ListViewItem item = new ListViewItem(info,fi.Name);
itemarr.Add(item);
//销毁图标
Win32.DestroyIcon(shfi.hIcon);
}
return itemarr;

}
catch
{
return null;
}
}
fanbingyuan 2010-12-31
  • 打赏
  • 举报
回复
Up Up 实在不行我就指定图标去。。。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧