【问题标题】:How to Uninstall the software programmatically in c#如何在 C# 中以编程方式卸载软件
【发布时间】:2018-10-11 15:59:49
【问题描述】:

如何在c#中以编程方式卸载软件?

Microsoft.Win32.RegistryKey Fregistry = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE")

            .OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion")
            .OpenSubKey("Installer").OpenSubKey("UserData")
            .OpenSubKey("S-1-5-18").OpenSubKey("Products");
            string[] Names = Fregistry.GetSubKeyNames();
            string uninstall = "";
            string ApplicationName = "Studio V5";
            for (int i = 0; i < Names.Length; i++)
            {
                Microsoft.Win32.RegistryKey FTemp = Fregistry.OpenSubKey(Names[i]).OpenSubKey("InstallProperties");
                **if (FTemp.GetValue("DisplayName").ToString() == ApplicationName)**
                {
                    object obj = FTemp.GetValue("UninstallString");
                    if (obj == null)
                        uninstall = "";
                    else
                        uninstall = obj.ToString();
                    i = Names.Length;
                }
            }

            System.Console.WriteLine(uninstall);
            System.Diagnostics.Process FProcess = new System.Diagnostics.Process();
            string temp = "/x{" + uninstall.Split("/".ToCharArray())[1].Split("I{".ToCharArray())[2];
            //replacing with /x with /i would cause another popup of the application uninstall
            FProcess.StartInfo.FileName = uninstall.Split("/".ToCharArray())[0];
            FProcess.StartInfo.Arguments = temp;
            FProcess.StartInfo.UseShellExecute = false;
            FProcess.Start();
            System.Console.Read();

我在 ** 行中遇到类似....NULL REFERENCE EXCEPTION 的错误。

【问题讨论】:

    标签: c#


    【解决方案1】:

    这是一种非常讨厌的方法,我怀疑它在任何情况下都能保证工作。相反,您应该使用Windows Installer API,特别是MsiConfigureProduct 和/或MsiConfigureFeature 函数与INSTALLSTATE_ABSENT 以编程方式卸载任何东西。

    【讨论】:

    • 这是否适用于未使用 Windows Installer 安装的软件?
    • 在这种情况下,Windows Installer 是一个 API,而不是任何特定的安装程序格式(例如 .msi)。非 .msi 安装程序仍然可以(并且应该)使用 WI API 来注册它安装的所有组件。如果它不这样做,那么它可能也不会在Microsoft\Installer 下写入注册表项,并且无论如何都无法发现。
    猜你喜欢
    • 2015-07-16
    • 2010-10-27
    • 2011-09-20
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 2019-05-11
    • 2015-03-05
    • 1970-01-01
    相关资源
    最近更新 更多