卸载时自定义操作(clickonce) - 在.NET中

时间:2009-07-22 05:04:31

标签: .net clickonce uninstall custom-action

对于使用ClickOnce安装的.NET应用程序,有没有办法在卸载过程中运行自定义操作。

具体来说,我需要删除一些与应用程序相关的文件(我在首次运行时创建)并在卸载过程中调用Web服务。

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

使用ClickOnce本身无法做到这一点,但您可以创建一个标准的Setup.exe引导程序来安装ClickOnce应用程序并具有自定义卸载操作。

请注意,这会在“添加/删除”程序中创建两个条目,因此您需要隐藏其中一个条目(clickonce应用程序)。

最后一个问题是clickonce上没有“静默卸载”选项,所以你可以这样做:

On Error Resume Next 

Set objShell = WScript.CreateObject("WScript.Shell")

objShell.Run "taskkill /f /im [your app process name]*"

objShell.Run "[your app uninstall key]"
Do Until Success = True
    Success = objShell.AppActivate("[your window title]")
    Wscript.Sleep 200
Loop
objShell.SendKeys "OK"

(找到here

答案 1 :(得分:2)

ClickOnce在HKEY_CURRENT_USER中安装卸载注册表项,ClickOnce应用程序可以访问该注册表项。

具体位置为" HKEY_CURRENT_USER \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall"

您必须使用应用程序的DisplayName搜索密钥。

然后,您可以包装正常的卸载操作

string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
Microsoft.Win32.RegistryKey uninstallKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(registryKey);
if (uninstallKey != null)
{
    foreach (String a in uninstallKey.GetSubKeyNames())
    {
        Microsoft.Win32.RegistryKey subkey = uninstallKey.OpenSubKey(a, true);
        // Found the Uninstall key for this app.
        if (subkey.GetValue("DisplayName").Equals("AppDisplayName"))
        {
            string uninstallString = subkey.GetValue("UninstallString").ToString();

            // Wrap uninstall string with my own command
            // In this case a reg delete command to remove a reg key.
            string newUninstallString = "cmd /c \"" + uninstallString +
                " & reg delete HKEY_CURRENT_USER\\SOFTWARE\\CLASSES\\mykeyv" +
                MYAPP_VERSION + " /f\"";
            subkey.SetValue("UninstallString", newUninstallString);
            subkey.Close();
        }
    }
}
相关问题