C#中卸载程序
C#的卸载代码比较简单, 当然你也可以用其他语言。
Process p = new Process(); p.StartInfo.FileName = "msiexec.exe"; p.StartInfo.Arguments = "/x {C56BBAC8-0DD2-4CE4-86E0-F2BDEABDD0CF} /quiet /norestart"; p.Start(); |
C#查找注册表中的ProductCode
最麻烦的在于,如何到注册表中获取ProductCode。 如果做非Web程序的自动化测试,经常需要跟注册表打交道。
代码为:
public static string GetProductCode(string displayName) { string productCode = string.Empty; // 如果是32位操作系统,(或者系统是64位,程序也是64位) string bit32 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; // 如果操作系统是64位并且程序是32位的 string bit64 = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"; RegistryKey localMachine = Registry.LocalMachine; RegistryKey Uninstall = localMachine.OpenSubKey(bit32, true); foreach (string subkey in Uninstall.GetSubKeyNames()) { RegistryKey productcode = Uninstall.OpenSubKey(subkey); try { string displayname = productcode.GetValue("DisplayName").ToString(); if (displayname == displayName) { string uninstallString = productcode.GetValue("UninstallString").ToString(); string[] strs = uninstallString.Split(new char[2] { '{', '}' }); productCode = strs[1]; return productCode; } } catch { } } return productCode; } |