获取系统中安装的应用程序的基本信息
来源:互联网 发布:数据库如何学 编辑:程序博客网 时间:2024/03/29 18:55
http://www.cnblogs.com/cssmystyle/archive/2011/01/25/1944551.html
当要实现获取系统中安装的应用程序基本信息时,很多人可能都会想到从注册表HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Uninstall里取,但这样会有几个弊端:1,取的应用程序不全。2,有的程序他没有安装路径只有卸载路径。3,有的程序获取不到Icon。为了解决问题1,我们可以想想咱们安装的程序在开始菜单=》所有程序中都会列出来。所以我们可以从这入手,获取开始菜单里的所有程序,当您打开开始菜单程序所在的文件夹时,发现里面全部是快捷方式。然后再看看右键快捷方式的属性,正好,启动路径和图标都有。这样那三个问题都可以帮我们解决了。
现在就把代码奉上,一起学习,一起进步:
001
public
static
void
LoadAppList(List<
string
> appNames, List<
string
> appPaths)
002
{
003
string
allUserStartMenuPath = GetAllUsersStartMenuPath() +
"//"
;
004
string
admStartMenuPath = GetUsersStartMenuPath() +
"//"
;
005
IShellLink shellLink = (IShellLink)
new
ShellLink();
006
UCOMIPersistFile vPersistFile = shellLink
as
UCOMIPersistFile;
007
List<
string
> temp =
new
List<
string
>();
008
List<
string
> appLnks =
new
List<
string
>();
009
GetInfo(appNames, appLnks, allUserStartMenuPath);
010
GetInfo(appNames, appLnks, admStartMenuPath);
011
012
for
(
int
j = 0; j < appLnks.Count; j++)
013
{
014
//获取快捷键运行路径
015
vPersistFile.Load(appLnks[j], 0);
016
StringBuilder stringBuilder =
new
StringBuilder(300);
017
WIN32_FIND_DATA vWIN32_FIND_DATA;
018
shellLink.GetPath(stringBuilder, stringBuilder.Capacity,
019
out
vWIN32_FIND_DATA, SLGP_FLAGS.SLGP_RAWPATH);
020
string
appPath = stringBuilder.ToString();
021
022
//过滤不想要的程序
023
if
(!appPath.ToLower().Contains(
"exe"
) || appPath.ToLower().Contains(
"unins"
)
024
|| appPath.ToLower().Contains(
"tool"
) || appPath.ToLower().Contains(
"config"
)
025
|| appPath.ToLower().Contains(
"{"
) || appPath.ToLower().Contains(
"%"
)
026
|| appPath.ToLower().Contains(
"plug"
) || appPath.ToLower().Contains(
"activex"
)
027
|| appPath.ToLower().Contains(
"help"
) || appPath.ToLower().Contains(
"extension"
)
028
|| appPath.ToLower().Contains(
"driver"
) || appPath.ToLower().Contains(
"system32"
)
029
|| appPath.ToLower().Contains(
"update"
) || appPath.ToLower().Contains(
"cmd"
)
030
|| appPath.ToLower().Contains(
"ati"
))
031
{
032
continue
;
033
}
034
if
(appNames[j].ToLower().Contains(
"command"
) || appNames[j].ToLower().Contains(
"卸载"
) || appNames[j].ToLower().Contains(
"logg"
) || appNames[j].ToLower().Contains(
"unins"
)
035
|| appNames[j].ToLower().Contains(
"setting"
) || appNames[j].ToLower().Contains(
"wizard"
)
036
|| appNames[j].ToLower().Contains(
"debugger"
) || appNames[j].ToLower().Contains(
"idea touch"
)
037
|| appNames[j].ToLower().Contains(
"touch-out"
))
038
{
039
continue
;
040
}
041
042
if
(temp.Contains(appPath))
043
{
044
continue
;
045
}
046
temp.Add(appPath);
047
appPaths.Add(appPath);
048
}
049
}
050
051
//获取当前文件夹下所有的快捷方式的名称和路径
052
private
static
void
GetInfo(List<
string
> appNames, List<
string
> appLnks,
string
path)
053
{
054
DirectoryInfo userFolder =
new
DirectoryInfo(path);
055
foreach
(DirectoryInfo NextFolder
in
userFolder.GetDirectories())
056
{
057
if
(NextFolder.Name.ToLower() ==
"touch game"
)
058
{
059
continue
;
060
}
061
if
(NextFolder.Attributes == FileAttributes.ReadOnly)
062
{
063
continue
;
064
}
065
foreach
(DirectoryInfo innerFolder
in
NextFolder.GetDirectories())
066
{
067
GetInfo(appNames, appLnks, NextFolder.FullName);
068
}
069
foreach
(FileInfo NextFile
in
NextFolder.GetFiles())
070
{
071
072
if
(NextFile.Extension.ToLower() !=
".lnk"
)
073
{
074
continue
;
075
}
076
string
name = NextFile.Name.Substring(0, NextFile.Name.LastIndexOf(
"."
));
077
if
(appNames.Contains(name))
078
{
079
continue
;
080
}
081
appNames.Add(name);
082
appLnks.Add(NextFile.FullName);
083
}
084
}
085
foreach
(FileInfo NextFolder
in
userFolder.GetFiles())
086
{
087
if
(NextFolder.Name.ToLower() ==
"touch game"
)
088
{
089
continue
;
090
}
091
if
(NextFolder.Extension.ToLower() !=
".lnk"
)
092
{
093
continue
;
094
}
095
string
name = NextFolder.Name.Substring(0, NextFolder.Name.LastIndexOf(
"."
));
096
if
(appNames.Contains(name))
097
{
098
continue
;
099
}
100
appNames.Add(name);
101
appLnks.Add(NextFolder.FullName);
102
103
}
104
}
下面是在网上找的一些获取快捷方式信息的方法:
001
[DllImport(
"shfolder.dll"
, CharSet = CharSet.Auto)]
002
private
static
extern
int
SHGetFolderPath(IntPtr hwndOwner,
int
nFolder, IntPtr hToken,
int
dwFlags, StringBuilder lpszPath);
003
private
const
int
MAX_PATH = 260;
004
private
const
int
CSIDL_COMMON_STARTMENU = 0x0017;
005
private
const
int
CSIDL_PROGRAMS = 0x0002;
006
007
/// <summary>
008
/// 获取本机All User开始程序路径(C:/Documents and Settings/All Users/「开始」菜单/程序)
009
/// </summary>
010
/// <returns></returns>
011
public
static
string
GetAllUsersStartMenuPath()
012
{
013
StringBuilder sbPath =
new
StringBuilder(MAX_PATH);
014
SHGetFolderPath(IntPtr.Zero, CSIDL_COMMON_STARTMENU, IntPtr.Zero, 0, sbPath);
015
return
sbPath.ToString();
016
}
017
/// <summary>
018
/// 获取本机管理员开始程序路径(C:/Documents and Settings/All Users/「开始」菜单/程序)
019
/// </summary>
020
/// <returns></returns>
021
public
static
string
GetUsersStartMenuPath()
022
{
023
StringBuilder sbPath =
new
StringBuilder(MAX_PATH);
024
SHGetFolderPath(IntPtr.Zero, CSIDL_PROGRAMS, IntPtr.Zero, 0, sbPath);
025
return
sbPath.ToString();
026
}
027
028
029
[Flags()]
030
public
enum
SLR_FLAGS
031
{
032
SLR_NO_UI = 0x1,
033
SLR_ANY_MATCH = 0x2,
034
SLR_UPDATE = 0x4,
035
SLR_NOUPDATE = 0x8,
036
SLR_NOSEARCH = 0x10,
037
SLR_NOTRACK = 0x20,
038
SLR_NOLINKINFO = 0x40,
039
SLR_INVOKE_MSI = 0x80
040
}
041
042
[Flags()]
043
public
enum
SLGP_FLAGS
044
{
045
SLGP_SHORTPATH = 0x1,
046
SLGP_UNCPRIORITY = 0x2,
047
SLGP_RAWPATH = 0x4
048
}
049
050
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
051
// Unicode version
052
public
struct
WIN32_FIND_DATA
053
{
054
public
int
dwFileAttributes;
055
public
FILETIME ftCreationTime;
056
public
FILETIME ftLastAccessTime;
057
public
FILETIME ftLastWriteTime;
058
public
int
nFileSizeHigh;
059
public
int
nFileSizeLow;
060
public
int
dwReserved0;
061
public
int
dwReserved1;
062
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
063
public
string
cFileName;
064
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
065
public
string
cAlternateFileName;
066
private
const
int
MAX_PATH = 260;
067
}
068
069
[
070
ComImport(),
071
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
072
Guid(
"000214F9-0000-0000-C000-000000000046"
)
073
]
074
075
// Unicode version
076
public
interface
IShellLink
077
{
078
void
GetPath(
079
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile,
080
int
cchMaxPath,
081
out
WIN32_FIND_DATA pfd,
082
SLGP_FLAGS fFlags);
083
084
void
GetIDList(
085
out
IntPtr ppidl);
086
087
void
SetIDList(
088
IntPtr pidl);
089
090
void
GetDescription(
091
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName,
092
int
cchMaxName);
093
094
void
SetDescription(
095
[MarshalAs(UnmanagedType.LPWStr)]
string
pszName);
096
097
void
GetWorkingDirectory(
098
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir,
099
int
cchMaxPath);
100
101
void
SetWorkingDirectory(
102
[MarshalAs(UnmanagedType.LPWStr)]
string
pszDir);
103
104
void
GetArguments(
105
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs,
106
int
cchMaxPath);
107
108
void
SetArguments(
109
[MarshalAs(UnmanagedType.LPWStr)]
string
pszArgs);
110
111
void
GetHotkey(
112
out
short
pwHotkey);
113
114
void
SetHotkey(
115
short
wHotkey);
116
117
void
GetShowCmd(
118
out
int
piShowCmd);
119
120
void
SetShowCmd(
121
int
iShowCmd);
122
123
void
GetIconLocation(
124
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,
125
int
cchIconPath,
126
out
int
piIcon);
127
128
void
SetIconLocation(
129
[MarshalAs(UnmanagedType.LPWStr)]
string
pszIconPath,
130
int
iIcon);
131
132
void
SetRelativePath(
133
[MarshalAs(UnmanagedType.LPWStr)]
string
pszPathRel,
134
int
dwReserved);
135
136
void
Resolve(
137
IntPtr hwnd,
138
SLR_FLAGS fFlags);
139
140
void
SetPath(
141
[MarshalAs(UnmanagedType.LPWStr)]
string
pszFile);
142
}
143
144
[
145
ComImport(),
146
Guid(
"00021401-0000-0000-C000-000000000046"
)
147
]
148
public
class
ShellLink
149
{
150
}
将上下两段代码放在一个类文件即可。。
xaml页面前台代码
01
<Window x:Class=
"WpfTest.GetApp.Window1"
02
xmlns=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
03
xmlns:x=
"http://schemas.microsoft.com/winfx/2006/xaml"
04
Title=
"Window1"
05
Height=
"500"
06
Width=
"610"
>
07
<Grid>
08
<Grid.ColumnDefinitions>
09
<ColumnDefinition Width=
"0.3*"
></ColumnDefinition>
10
<ColumnDefinition Width=
"0.7*"
></ColumnDefinition>
11
</Grid.ColumnDefinitions>
12
<ListBox x:Name=
"lstAppNames"
13
Grid.Column=
"0"
></ListBox>
14
<ListBox x:Name=
"lstAppPaths"
15
Grid.Column=
"1"
></ListBox>
16
</Grid>
17
</Window>
xaml页面后台代码
01
public
partial
class
Window1 : Window
02
{
03
public
Window1()
04
{
05
InitializeComponent();
06
Loaded +=
new
RoutedEventHandler(Window1_Loaded);
07
}
08
09
void
Window1_Loaded(
object
sender, RoutedEventArgs e)
10
{
11
List<
string
> appNames =
new
List<
string
>();
12
List<
string
> appPaths =
new
List<
string
>();
13
GetApp.LoadAppList(appNames, appPaths);
//调用获取App的方法
14
lstAppNames.ItemsSource = appNames;
15
lstAppPaths.ItemsSource = appPaths;
16
}
17
18
}
- 获取系统中安装的应用程序的基本信息
- 获取系统中安装的应用程序的基本信息
- 获取系统的一些基本信息
- java获取系统的基本信息
- 获取系统内Applications的基本信息
- 获取系统内Applications的基本信息
- 获取Linux(redhat)系统的一些基本信息
- 获取未安装APK的一些基本信息
- 获取Android系统中应用程序的信息
- 安卓中获取应用程序基本信息
- [C#学习笔记]获取当前系统的基本信息
- 获取手机中安装的所有应用程序
- 获取手机中安装的所有应用程序
- 获取手机中安装的所有应用程序
- 获取手机中安装的所有应用程序
- 获取手机中安装的所有应用程序
- Android中获取手机联系人的基本信息的实现
- Android获取手机的基本信息
- 关于.NET安装时需要重启动的处理办法,以及延伸出的重启后继续安装的安装包的一点想法
- C# Asp.Net进度条
- 1
- IOCP
- ASP.NET AJAX中UpdatePanel的工作原理
- 获取系统中安装的应用程序的基本信息
- MSSQL 时间格式
- Silverlight - 用From/To/By制作基本动画
- android 的好blog
- Cacti安装磁盘IO监控
- .net获取IP地址的几种方法
- 【转】用十年教会自己编程
- 三十分钟掌握STL
- C#获取用户桌面等特殊系统路径