C# 卸载程序

C# 卸载程序,c#,C#,我正在尝试卸载具有此代码的程序。。但它似乎不起作用。我试过其他答案,但似乎也不管用。。有人能帮我吗?我正试图以给定名称(displayName)卸载该程序 例如,我给出displayName=Appname,那么这段代码应该从我的计算机上卸载Appname程序 public static void UninstallApplictionInstalled(string p_name) { string displayName; string uninstl

我正在尝试卸载具有此代码的程序。。但它似乎不起作用。我试过其他答案,但似乎也不管用。。有人能帮我吗?我正试图以给定名称(displayName)卸载该程序

例如,我给出displayName=Appname,那么这段代码应该从我的计算机上卸载Appname程序

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"));
            }
        }
      }
只有这个会弹出。。

重复:欢迎来到Stackoverflow。我只想告诉你们,我看到这个问题至少有三种不同的说法。我们将不得不结束您的一些问题,因为重复会分散答复,如果人们回答(似乎)未回答的重复,可能会浪费大量时间

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


C#:使用C#因为这可能很笨重——不管你怎么做。我不会将命令行推送到msiexec.exe,而是直接通过。可以通过或访问此API

为MSI卸载Appraches:为您提供参考,有无数种方法可以启动MSI 卸载:

<强>第14部分<强>上面的链接显示了如何使用C++卸载——如果这是一个选项。但是:,Visual Studio 2017模板再次发生了更改,因此可能需要进行调整才能“开箱即用”

但是,我会使用MSI API(如前所述),我建议您使用本机Win32函数,并使用WiX工具包的一部分(部署工具基础)。它是MSI API的.NET包装器-这将为您节省大量样板代码,而您必须将DTF DLL:Microsoft.Deployment.WindowsInstaller.DLL与产品一起部署。我不知道这是否可以接受。我的代码不依赖于DTF(如果需要的话),但它要长得多

模型C#样本。需要对Microsoft.Deployment.WindowsInstaller.dll的项目引用。然后在新的C#.NET项目中尝试以下代码。您可以通过安装开源工具包来创建MSI文件,从而获得该DLL。安装后签入
%ProgramFiles(x86)%\WiX工具集v3.11\bin
(根据WiX版本进行调整-自2018年9月起生效)

安装程序GUI:首先要注意:安装程序的UI级别是通过
安装程序.SetInternalUI
功能设置的。如果在静默模式下运行,则需要运行提升的可执行文件以使卸载正常工作,否则会发生访问异常。当您在全GUI模式下运行时,您需要自行升级安装—前提是您有权这样做

提升运行:如何检查管理员权限:


你确定你的应用程序有卸载权限吗?你是以管理员的身份运行这个吗?另外,您是否尝试过在没有/quiet和启用/L日志记录的情况下运行该程序。将
UninstallString
放置在
UninstallString中是一个完整的独立命令,用于卸载该程序。无法保证
msiexec/x
是卸载任何给定程序的正确方法,即使是这样,
UninstallString
也将包含该文本。请告诉我们
似乎不起作用的意思是什么?如果您能给我们一些运行此程序时收到的“错误消息”,那将更有帮助。当我阅读您的代码时,我首先想到的是
void IsApplicationInstalled
。考虑到这个名字,我本以为是
bool
而不是
void
。函数的主体也与它的名称完全不匹配。就像你问某人“嘿,你有牛奶吗?”他回答“嗯,让我检查一下……是的,我刚把它倒在厕所里”或者更糟:他把它倒在厕所里,什么都不告诉你。(只是说)弹出窗口意味着命令的语法错误。您是否提供了与该/x或显示名称配套的msi/产品代码?显示名称不同。
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;
        }
    }
}