卸载程序 [英] Uninstalling program

查看:92
本文介绍了卸载程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此代码卸载程序。但是它似乎不起作用。我尝试了其他答案,但似乎也没有用。有人可以帮助我吗?我正在尝试使用给定名称(displayName)卸载程序

I'm trying to uninstall a program with this code.. But it doesn't seem to work. I've tried the other answers but didn't seem to work either.. Can someone help me with this? I'm trying to uninstall the program by a given name(displayName)

例如,我输入displayName = Appname,则此代码应从我的计算机上卸载该Appname程序。

For example I give the displayName = Appname then this code should uninstall the Appname program from my computer.

public static void UninstallApplictionInstalled(string p_name)
    {
        string displayName;
        string uninstlString;
        RegistryKey key;

        ProcessStartInfo info = new ProcessStartInfo();
        Process uninstallProcess = new Process();
        string temp;

        // search in: CurrentUser
        key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
        foreach (String keyName in key.GetSubKeyNames())
        {
            RegistryKey subkey = key.OpenSubKey(keyName);
            displayName = Convert.ToString(subkey.GetValue("DisplayName"));
            uninstlString = Convert.ToString(subkey.GetValue("UninstallString"));

            if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
            {
                
                uninstallProcess.StartInfo.FileName = "MsiExec.exe";
                uninstallProcess.StartInfo.Arguments = " /x " + uninstlString + " /quiet /norestart";
                uninstallProcess.Start();
                uninstallProcess.WaitForExit();
                break;

                //Console.WriteLine(subkey.GetValue("UninstallString"));
            }
        }

        // search in: LocalMachine_32
        key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
        foreach (String keyName in key.GetSubKeyNames())
        {
            RegistryKey subkey = key.OpenSubKey(keyName);
            displayName = Convert.ToString(subkey.GetValue("DisplayName"));
            uninstlString = Convert.ToString(subkey.GetValue("UninstallString"));

            if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
            {
                uninstallProcess.StartInfo.FileName = "MsiExec.exe";
                uninstallProcess.StartInfo.Arguments = " /x " + uninstlString + " /quiet /norestart";
                uninstallProcess.Start();
                uninstallProcess.WaitForExit();
                break;

                //Console.WriteLine(subkey.GetValue("UninstallString"));
            }
        }

        // search in: LocalMachine_64
        key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
        foreach (String keyName in key.GetSubKeyNames())
        {
            RegistryKey subkey = key.OpenSubKey(keyName);
            displayName = Convert.ToString(subkey.GetValue("DisplayName"));
            uninstlString = Convert.ToString(subkey.GetValue("UninstallString"));

            if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
            {
                //string prdctId = uninstlString.Substring((uninstlString.IndexOf("{")));

                uninstallProcess.StartInfo.FileName = "MsiExec.exe";
                uninstallProcess.StartInfo.Arguments = " /x " + uninstlString + " /quiet /norestart";
                uninstallProcess.Start();
                uninstallProcess.WaitForExit();
                break;
                //Console.WriteLine(subkey.GetValue("UninstallString"));
            }
        }
      }

仅此弹出窗口。.

Only this pops up..

推荐答案

重复 :欢迎使用Stackoverflow。顺便提一下,我看到至少有3种不同口味的问题。我们将不得不关闭您的一些问题,因为重复会分散答复,并且如果人们回答(貌似)未回答的重复项,会浪费很多时间。

Duplicates: Welcome to Stackoverflow. Just to mention to you that I see this question asked in at least 3 different flavors. We will have to close some of your questions since the duplication scatters replies and can waste a lot of time if people answer the (seemingly) unanswered duplicates.

简而言之:请不要多次发布相同的问题。以下是其他问题:

In short: please don't post the same question several times. Here are the other questions:

  • MsiExec.exe product id uninstall
  • MSI installer option- uninstalling an application

C# :为此,使用C#可能很麻烦-不管您如何做。我不会将命令行推送到 msiexec.exe ,而是直接通过 MSI API 。可以通过 Win32函数 COM自动化

C#: Using C# for this can be clunky - no matter how you do it. I would not push a command line to msiexec.exe, but go directly via the MSI API. This API can be accessed via Win32 functions or COM automation.


为MSI卸载App :您的参考资料中,有多种方法可以启动MSI
卸载:
在不使用msiexec的情况下从命令行卸载MSI文件

<上面链接中的strong>第14节显示了如何使用C ++卸载-如果可以的话。但是:在Visual Studio 2017模板中又有更改,因此可能需要进行调整才能开箱即用。

Section 14 from the link above shows how to uninstall using C++ - if that is an option. However:, there are changes in the Visual Studio 2017 templates again, so it might need a tune-up to work "out-of-the-box".

但是,我将使用MSI API(如前所述),并且我建议您通过本机Win32函数进行操作,并建议使用 DTF (部署工具基金会),它是WiX工具包的一部分。它是MSI API的.NET包装器-可以节省许多样板代码,而不得不部署DTF DLL: Microsoft.Deployment.WindowsInstaller.dll 以及您的产品。我不知道这是否可以接受。我有一些不需要依赖DTF的代码,但是它更长。

However, I would use the MSI API - as already stated - and I would recommend you go via the native Win32 functions and that you use DTF (Deployment Tools Foundation) which is part of the WiX toolkit. It is a .NET wrapper for the MSI API - which will save you a lot of boilerplate code, at the expense of having to deploy the DTF DLL: Microsoft.Deployment.WindowsInstaller.dll along with your product. I do not know if this is acceptable. I have code that does not depend on DTF if need be, but it is much longer.

模拟C#示例 。需要对 Microsoft.Deployment.WindowsInstaller.dll 的项目引用。然后在一个新的C#.NET项目中尝试以下代码。您可以通过安装> WiX工具包 来获取该DLL。创建MSI文件。安装后,请检入 %ProgramFiles(x86)%\WiX Toolset v3.11\bin (适用于WiX版本-截至9月) 2018)。

Mock-up C# Sample. Project reference to Microsoft.Deployment.WindowsInstaller.dll needed. Then try the below code in a fresh C# .NET project. You can get that DLL by installing the WiX toolkit - the open source toolkit to create MSI files. After installation check in %ProgramFiles(x86)%\WiX Toolset v3.11\bin (adjust for WiX version - current as of September 2018).

安装程序GUI :首先重要的注意事项:设置的 UI级别通过 Installer.SetInternalUI 函数进行设置。如果以静默方式运行,则需要运行提升的可执行文件才能使卸载正常工作,否则会发生访问异常。在完全GUI模式下运行时,您需要自己提升安装-只要您有权利这样做。

Installer GUI: Important note first: the setup's UI level is set via the Installer.SetInternalUI function. If you run in silent mode, then you need to run the executable elevated for the uninstall to work properly, or an access exception occurs. When you run in Full GUI mode, you need to elevate the install yourself - provided you have the rights to do so.


高架运行 :如何检查管理员权限: 检查当前用户是否为管理员

Run Elevated: How to check for admin rights: Check if the current user is administrator.



using System;
using Microsoft.Deployment.WindowsInstaller;

namespace UninstallMsiViaDTF
{
    class Program
    {
        static void Main(string[] args)
        {
            // Update this name to search for your product. This sample searches for "Orca"
            var productcode = FindProductCode("orca");

            try
            {
                if (String.IsNullOrEmpty(productcode)) { throw new ArgumentNullException("productcode"); }

                // Note: Setting InstallUIOptions to silent will fail uninstall if uninstall requires elevation since UAC prompt then does not show up 
                Installer.SetInternalUI(InstallUIOptions.Full); // Set MSI GUI level (run this function elevated for silent mode)
                Installer.ConfigureProduct(productcode, 0, InstallState.Absent, "REBOOT=\"ReallySuppress\"");

                // Check: Installer.RebootInitiated and Installer.RebootRequired;
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }

            Console.ReadLine(); // Keep console open
        }

        // Find product code for product name. First match found wins
        static string FindProductCode(string productname)
        {
            var productcode = String.Empty;

            foreach (ProductInstallation product in ProductInstallation.AllProducts)
            {
                if (product.ProductName.ToLower().Contains(productname.ToLower()))
                {
                    productcode = product.ProductCode;
                    break;
                }
            }

            return productcode;
        }
    }
}

这篇关于卸载程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆