How to uninstall application programmatically

女生的网名这么多〃 提交于 2019-12-30 14:30:48

问题


I tried this,this to uninstall the application programmatically. I am not getting any error or exception but the application is not uninstalled from my machine. Please see tried code also

public static string GetUninstallCommandFor(string productDisplayName)
{
    RegistryKey localMachine = Registry.LocalMachine;
    string productsRoot = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products";
    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(productDisplayName)))
            {
                string uninstallCommand =(string)installProperties.GetValue("UninstallString");

                return uninstallCommand;
            }
        }
    }

    return "";        
}

Please help me to uninstall the application programmatically using C#.


回答1:


The above routine will return a string, assuming it found a match that may look like:

MsiExec.exe /X{02DA0248-DB55-44A7-8DC6-DBA573AEEA94}

You need to take that and run it as a process:

System.Diagnostics.Process.Start(uninstallString);

Note that it may not be always msiexec, it can be anything that the program chooses to specify. In case of msiexec, you can append /q parameter to your uninstallString to make it uninstall silently (and it won't show those Repair/Remove dialogs).

Update: If you're using Windows installer 3.0 or above, you can also use /quiet for silent install/uninstall. It's basically same as /qn (if you're on older versions). Source. Thanks @JRO for bringing it up!



来源:https://stackoverflow.com/questions/26720065/how-to-uninstall-application-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!