【问题标题】:How to uninstall a program using C#? [duplicate]如何使用 C# 卸载程序? [复制]
【发布时间】:2015-08-31 13:48:52
【问题描述】:

我正在尝试通过 Visual Studio 和可能的 CMD 卸载使用 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 2012。代码目前正在从 main 方法运行。感谢您的帮助。

【问题讨论】:

    标签: c# visual-studio visual-studio-2012 command-line windows-installer


    【解决方案1】:

    您已使用 Windows Installer 标记进行询问,因此,如果我们谈论的是从 MSI 文件安装的产品:

    尝试 1 不正确,因为 Windows Installer 不使用卸载字符串来卸载产品(更改它,看看它是否有影响),并且有更好的方法。

    2 使用 WMI,你可以让它工作,但同样没有必要。

    我假设您知道要卸载的东西的 ProductCode,如果您以后不知道的话。所以有一个非常好的普通 API 来卸载产品,MsiConfigureProduct(),这里有一些例子:

    How to uninstall MSI using its Product Code in c#

    以及将 msiexec.exe 与 ProductCode 一起使用的方法。

    如果您需要枚举所有已安装的产品以扫描名称或其他内容,请参阅:

    How to get a list of installed software products?

    【讨论】:

    • 链接 1 和 2 让我得到了我需要的东西!
    猜你喜欢
    • 1970-01-01
    • 2017-08-09
    • 2012-03-24
    • 1970-01-01
    • 2013-09-12
    • 2011-08-06
    • 2011-04-26
    • 2010-09-11
    • 2021-05-06
    相关资源
    最近更新 更多