0

我试图通过Visual Studio和可能的CMD卸载使用C#的程序。我做了几次尝试,但没有得到任何东西。如何使用C#卸载程序?

尝试#1:

 RegistryKey localMachine = Registry.LocalMachine; 
     string productsRoot = @"C:\Program Files(x86)\Microsoft\XML"; 
     RegistryKey products = localMachine.OpenSubKey(productsRoot); 
     string[] productFolders = products.GetSubKeyNames(); 

     foreach (string p in productFolders) 
     { 
      RegistryKey installProperties = products.OpenSubKey(p + @"\InstallProperties"); 
      if (installProperties != null) 
      { 
       string displayName = (string)installProperties.GetValue("DisplayName"); 
       if ((displayName != null) && (displayName.Contains("XML"))) 
       { 
        string uninstallCommand = (string)installProperties.GetValue("UninstallString"); 
        return uninstallCommand; 
       } 
      } 
     } 

基于:https://sites.google.com/site/msdevnote/home/programmatically-uninstall-programs-with-c

尝试#2:基于

Process p = new Process(); 
    ProcessStartInfo info = new ProcessStartInfo(); 
    info.FileName = "cmd.exe"; 
    info.RedirectStandardInput = true; 
    info.UseShellExecute = false; 

    p.StartInfo = info; 
    p.Start(); 

    using (StreamWriter sw = p.StandardInput) 
    { 
     if (sw.BaseStream.CanWrite) 
     { 
      sw.WriteLine("wmic"); 
      sw.WriteLine("product get name"); 
      sw.WriteLine("XML" call uninstall); 
     } 
    } 

http://www.sevenforums.com/tutorials/272460-programs-uninstall-using-command-prompt-windows.htmlExecute multiple command lines with the same process using .NET

我使用Visual Studio代码从主要我运行现在就来。谢谢你的帮助。

回答

1

你问一个Windows安装程序标签,所以如果我们谈论的是从MSI文件安装的产品:

尝试1不正确,因为Windows安装程序不使用uninstallstring卸载产品(改变它,看看它是否有所作为),并有更好的方法。

2使用WMI,你可以做这个工作,但它又是没有必要的。

我打算假设您知道要卸载的产品的ProductCode,如果以后不再那么做。所以这是一个非常好的正常的API来卸载产品,MsiConfigureProduct(),这里也有这样的例子:使用msiexec.exe的与产品代码的

How to uninstall MSI using its Product Code in c#

以及方式。

如果您需要枚举所有已安装的产品扫描名称或东西,然后看到:

How to get a list of installed software products?

+0

链接1和2有我我需要的东西! – sam