设置卸载并添加/删除程序卸载



有两种方法可以卸载我的应用程序。

  1. 使用同一设置。
  2. 添加/删除程序控制面板中

我们在设置中有一个特殊的卸载过程,它启动了一些特殊的对话框以获取用户输入。这样,卸载根据用户输入进行。但是问题在于,如果您使用" add/删除程序"卸载它,则特殊的卸载过程将无法执行。有没有一种方法可以启动特定于应用程序的卸载,但"添加/删除程序"?

如果您使用的是基于MSI的项目,则卸载按钮将在被动模式下运行卸载。因此,将跳过UI或对话顺序中的任何操作。要解决此问题,通常禁用卸载按钮(请参阅ARPNOREMOVE),并要求最终用户通过修改按钮(确实显示UI)。

您可以使用WMI进行操作。您可以根据需要自定义卸载软件。为了实现这一目标,您必须使用Win32_Product类和卸载方法。以下是在本地计算机上卸载程序的示例:

using System;
using System.Management;
using System.Windows.Forms;
namespace WMISample
{
    public class CallWMIMethod
    {
        public static void Main()
        {
            try
            {
                ManagementObject classInstance =
                    new ManagementObject("root\CIMV2",
                    "Win32_Product.IdentifyingNumber='{EDDE41A3-A870-4D97-A1ED-67FF62AA0552}',Name='MyServiceSetup',Version='1.0.0'",
                    null);
                // No method in-parameters to define

                // Execute the method and obtain the return values.
                ManagementBaseObject outParams =
                    classInstance.InvokeMethod("Uninstall", null, null);
                // List outParams
                Console.WriteLine("Out parameters:");
                Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
            }
            catch(ManagementException err)
            {
                MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
            }
        }
    }
}

您可以在错误代码上检查返回值 Windows Desktop Apps )。

最新更新