using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32; //引入注册表命名空间
using System.Collections;
namespace 获取安装软件和路径
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//哈希表保存软件名称和路径
Hashtable ht = new Hashtable();
private void Form1_Load(object sender, EventArgs e)
{
RegistryKey rkey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall", false);
if (rkey != null)//判断对象存在
{
foreach (string keyName in rkey.GetSubKeyNames())//遍历子项名称的字符串数组
{
using (RegistryKey subKey = rkey.OpenSubKey(keyName, false))//遍历子项节点
{
if (subKey != null)
{
//可以自己选择要显示的项除了软件名地址还有其他的几项
//获取安装的软件名
string softName = subKey.GetValue("DisplayName", "").ToString();
//获取安装路径安装路径
string softLocation = subKey.GetValue("InstallLocation", "").ToString();
if (!string.IsNullOrEmpty(softLocation)&&!string.IsNullOrEmpty(softName))
{
ht.Add(softName, softLocation);
listBoxSoftName.Items.Add(softName);
}
}
}
}
}
}
///
/// 根据软件名称显示安装路径
///
private void listBoxSoftName_SelectedIndexChanged(object sender, EventArgs e)
{
string softName = listBoxSoftName.SelectedItem.ToString();
this.lblPathByName.Text = ht[softName].ToString();
}
}
}
取消
评论