c# 获取系统文件图标
最近写一个小程序,需要获得启动项的信息,在读取启动程序图标时遇到了这个问题
我们知道在c#里添加图标最常用的方法是使用ImageList,但要读取的程序图标是动态的,那么就需要动态的获得文件的图标
在网上找了找,发现有一个类,直接使用就可以了
贴出来和大家共享,本人在写一个类似于360safe的东西,不过软件增加了更多的个人色彩,希望大家能多多交流!
showSystemIcon.cs
使用:
把它添加到ListView中
有什么问题大家交流交流吧!
我们知道在c#里添加图标最常用的方法是使用ImageList,但要读取的程序图标是动态的,那么就需要动态的获得文件的图标
在网上找了找,发现有一个类,直接使用就可以了
贴出来和大家共享,本人在写一个类似于360safe的东西,不过软件增加了更多的个人色彩,希望大家能多多交流!
showSystemIcon.cs
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
using System.Runtime.InteropServices;
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
namespace RepairTools_2._0
11
{
12
class showSystemIcon
13
{
14
public static uint SHGFI_ICON = 0x100;
15
public static uint SHGFI_DISPLAYNAME = 0x200;
16
public static uint SHGFI_TYPENAME = 0x400;
17
public static uint SHGFI_ATTRIBUTES = 0x800;
18
public static uint SHGFI_ICONLOCATION = 0x1000;
19
public static uint SHGFI_EXETYPE = 0x2000;
20
public static uint SHGFI_SYSICONINDEX = 0x4000;
21
public static uint SHGFI_LINKOVERLAY = 0x8000;
22
public static uint SHGFI_SELECTED = 0x10000;
23
public static uint SHGFI_LARGEICON = 0x0;
24
public static uint SHGFI_SMALLICON = 0x1;
25
public static uint SHGFI_OPENICON = 0x2;
26
public static uint SHGFI_SHELLICONSIZE = 0x4;
27
public static uint SHGFI_PIDL = 0x8;
28
public static uint SHGFI_USEFILEATTRIBUTES = 0x10;
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
public static uint FILE_ATTRIBUTE_NORMAL = 0x80;
31
public static uint LVM_FIRST = 0x1000;
32
public static uint LVM_SETIMAGELIST = LVM_FIRST + 3;
33
public static uint LVSIL_NORMAL = 0;
34
public static uint LVSIL_SMALL = 1;
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
[DllImport("Shell32.dll")]
37
public static extern IntPtr SHGetFileInfo(string pszPath,
38
uint dwFileAttributes, ref SHFILEINFO psfi,
39
int cbfileInfo, uint uFlags);
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
public struct SHFILEINFO
42
{
43
public IntPtr hIcon;
44
public int iIcon;
45
public int dwAttributes;
46
public string szDisplayName;
47
public string szTypeName;
48
}
49![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
[DllImport("User32.DLL")]
51
public static extern int SendMessage(IntPtr hWnd,
52
uint Msg, IntPtr wParam, IntPtr lParam);
53![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
public void ListViewSysImages(ListView AListView)
55
{
56
SHFILEINFO vFileInfo = new SHFILEINFO();
57
IntPtr vImageList = SHGetFileInfo("", 0, ref vFileInfo,
58
Marshal.SizeOf(vFileInfo), SHGFI_SHELLICONSIZE |
59
SHGFI_SYSICONINDEX | SHGFI_LARGEICON);
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
SendMessage(AListView.Handle, LVM_SETIMAGELIST, (IntPtr)LVSIL_NORMAL,
62
vImageList);
63![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
vImageList = SHGetFileInfo("", 0, ref vFileInfo,
65
Marshal.SizeOf(vFileInfo), SHGFI_SHELLICONSIZE |
66
SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
67
SendMessage(AListView.Handle, LVM_SETIMAGELIST, (IntPtr)LVSIL_SMALL,
68
vImageList);
69
}
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
public int FileIconIndex(string AFileName)
72
{
73
SHFILEINFO vFileInfo = new SHFILEINFO();
74
SHGetFileInfo(AFileName, 0, ref vFileInfo,
75
Marshal.SizeOf(vFileInfo), SHGFI_SYSICONINDEX);
76
return vFileInfo.iIcon;
77
}
78
}
79
}
80![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
使用:
把它添加到ListView中
1
private void btnRunSearch_Click(object sender, EventArgs e)
2
{
3
try
4
{
5
ListViewItem lvi;
6
ListViewItem.ListViewSubItem lvsi;
7
showSystemIcon showIcon = new showSystemIcon();
8![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
this.lvStartupFileList.Items.Clear();
10
lvStartupFileList.BeginUpdate();
11
foreach (string str in RegistryOperate.strRunRegistry())
12
{
13
string str1 = RegistryOperate.returnRun().GetValue(str).ToString();
14
showIcon.ListViewSysImages(lvStartupFileList);
15
lvi = new ListViewItem();
16
lvi.Text = str;
17
lvi.ImageIndex = showIcon.FileIconIndex(str1);
18
lvsi = new ListViewItem.ListViewSubItem();
19
lvsi.Text = str1;
20
lvi.SubItems.Add(lvsi);
21
lvStartupFileList.Items.Add(lvi);
22
}
23
lvStartupFileList.EndUpdate();
24
}
25
catch
26
{
27
MessageBox.Show(e.ToString());
28
}
29
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
有什么问题大家交流交流吧!