本文实例讲述了C#通过windows注册表获取软件清c#教程单的方法。分享给大家供大家参考。具体如下:
foreach (string SoftwareName in Object.SoftwareList())
{
textBox.Text += SoftwareName + Environment.NewLine;
}
////////////////////////////////////////////////////////////////////////
/// <summary>
/// Windows系统获取软件列表
/// </summary>
/// <returns>String [] softwareList</returns>
public String [] SoftwareList()
{
String[] softwareList = null;
//动态数组
ArrayList list = new ArrayList();
try
{
//打开注册列表卸载选项
//SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
RegistryKey Key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
if (Key != null)//如果系统禁止访问则返回null
{
foreach (String SubKeyName in Key.GetSubKeyNames())
{
//打开对应的软件名称
RegistryKey SubKey = Key.OpenSubKey(SubKeyName);
if (SubKey != null)
{
String SoftwareName = SubKey.GetValue("DisplayName", "Nothing").ToString();
//如果没有取到,则不存入动态数组
if (SoftwareName != "Nothing")
{
list.Add(SoftwareName);
}
}
}
//强制转换成字符串数组,防止被修改数据溢出
softwareList = (string[])list.ToArray(typeof(string));
}
}
catch (Exception err)
{
Console.WriteLine("出错信息:" + err.ToString());
}
return softwareList;
}
foreach (string SoftwareName in Object.SoftwareList())
{
textBox.Text += SoftwareName + Environment.NewLine;
}
////////////////////////////////////////////////////////////////////////
/// <summary>
/// Windows系统获取软件列表
/// </summary>
/// <returns>String [] softwareList</returns>
public String [] SoftwareList()
{
String[] softwareList = null;
//动态数组
ArrayList list = new ArrayList();
try
{
//打开注册列表卸载选项
//SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
RegistryKey Key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
if (Key != null)//如果系统禁止访问则返回null
{
foreach (String SubKeyName in Key.GetSubKeyNames())
{
//打开对应的软件名称
RegistryKey SubKey = Key.OpenSubKey(SubKeyName);
if (SubKey != null)
{
String SoftwareName = SubKey.GetValue("DisplayName", "Nothing").ToString();
//如果没有取到,则不存入动态数组
if (SoftwareName != "Nothing")
{
list.Add(SoftwareName);
}
}
}
//强制转换成字符串数组,防止被修改数据溢出
softwareList = (string[])list.ToArray(typeof(string));
}
}
catch (Exception err)
{
Console.WriteLine("出错信息:" + err.ToString());
}
return softwareList;
}
希望本文python基础教程所述对大家的C#程序设计有所帮助。