一个窗体,标签,按钮,列表视图
列表视图
属性
1设置View属性为 Details
2设置GridLines为True ,显示表格线
3设置FullRowSelect为True,单击某个单元格选中一行。
设置列名为,序号、软件、路径、参数
//卸载
private void button1_Click(object sender, EventArgs e)
{
string path = this.listv2.FocusedItem.SubItems[2].Text;//选中行第二个的参数,
string para = this.listv2.FocusedItem.SubItems[3].Text;//选中行第三个的参数,
Process p = new Process();//创建进程对象
p.StartInfo.FileName = path;//获取应用独有的卸载软件路径
p.StartInfo.Arguments = para;//获取应用独有的卸载参数
p.Start();//开始执行
}
//刷新
private void button3_Click(object sender, EventArgs e)
{
UnService();
}
//获取选中
private void button2_Click(object sender, EventArgs e)
{
//获取用户当前选择的行的数据
try
{
string name = this.listv2.FocusedItem.SubItems[1].Text;
//获取选中行,选中行集合里面第几列的文本,这里是第二列的文本
MessageBox.Show(name);
} catch { }
}
/// <summary>
/// 软件卸载信息实体
/// </summary>
public class SoftUninstallItem
{
/// <summary>
/// 软件名称
/// </summary>
public string SoftName { get; set; }
/// <summary>
/// 软件路径
/// </summary>
public string UninstallPath { get; set; }
/// <summary>
/// 参数
/// </summary>
public string UnitstallPara { get; set; }
public override string ToString()
{
return string.Format("软件名称:{0} 卸载路径:{1} {2}", SoftName, UninstallPath, UnitstallPara);
}
}
/// <summary>
/// 获取卸载路径和卸载参数
/// </summary>
/// <param name="uninstallString"></param>
/// <param name="ParaName"></param>
/// <returns></returns>
private string getPath(string uninstallString, out string ParaName)//输入卸载路径字符串,返回卸载参数,总返回卸载路径
{
ParaName = "";
int space = uninstallString.IndexOf("."); //获取路径中.exe中的.的位置索引
if (space < 1) return uninstallString.Trim(' ', '"');
space = uninstallString.IndexOf(" ", space);//获取路径中.exe 后的第一个空格
if (space < 1) return uninstallString;
ParaName = uninstallString.Substring(space + 1).Trim(' ', '"'); //空格后面就是参数
return uninstallString.Substring(0, space).Trim(' ', '"'); //空格前面是卸载程序路径
}
//刷新方法
private void UnService()
{
listBox1.Items.Clear();//清空列表框
string temp = null, tempType = null;
object displayName = null, uninstallString = null, releaseType = null;
RegistryKey currentKey = null;//注册表基础项子项
int softNum = 0;//总数为0
RegistryKey pregkey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");//获取指定路径下的子项
try
{
foreach (string item in pregkey.GetSubKeyNames()) //获取基础项子项的所有子项的名称
{
currentKey = pregkey.OpenSubKey(item);//用子项的名称打开子项
displayName = currentKey.GetValue("DisplayName"); //获取应用名称
uninstallString = currentKey.GetValue("UninstallString"); //获取卸载字符串路径
releaseType = currentKey.GetValue("ReleaseType"); //发行类型,值是Security Update为安全更新,Update为更新
bool isSecurityUpdate = false;//更新类型为假
if (releaseType != null)更新类型不为空
{
tempType = releaseType.ToString();//获取更新类型
if (tempType == "Security Update" || tempType == "Update")
isSecurityUpdate = true;//更新类型不为空
}
if (!isSecurityUpdate && displayName != null && uninstallString != null)//更新类型和应用名称和卸载路径都不是空的
{
softNum++;//总数,加加
temp = displayName.ToString() + Environment.NewLine;
SoftUninstallItem Uninitem = new SoftUninstallItem();//创建卸载软件的信息实体
Uninitem.SoftName = temp.Trim();//实体获取软件名称
string paraName;
Uninitem.UninstallPath = getPath(uninstallString.ToString().Trim(), out paraName);//实体获取软件路径
Uninitem.UnitstallPara = paraName;
listBox1.Items.Add(Uninitem);//添加实体
//listBox1.Items.Add(Environment.NewLine + temp + " 文件路径:" + uninstallString.ToString());
//textBox1.Text = Environment.NewLine + uninstallString.ToString();
}
}
}
catch (Exception E)
{
MessageBox.Show(E.Message.ToString());//显示报错信息
}
label1.Text = "共有软件" + softNum.ToString() + "个";//显示软件总数
//listBox1.Items.Add("共有软件" + softNum.ToString() + "个");
//textBox1.Text = "共有软件" + softNum.ToString() + "个" + Environment.NewLine + temp;
pregkey.Close();//关闭注册表
}
private void 列表视图_Load(object sender, EventArgs e)
{
UnService();
}