卸载时的自定义操作(单击一次)- 在 .NET 中

发布于 2024-07-27 04:52:19 字数 142 浏览 4 评论 0原文

对于使用 ClickOnce 安装的 .NET 应用程序,是否有任何方法可以在卸载过程中运行自定义操作。

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

有任何想法吗?

For a .NET application installed using ClickOnce, is there any way to run a custom action during the uninstall process.

Specifically, I need to delete a few app related files (which I created on first run) and call a web service during the uninstall process.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯 2024-08-03 04:52:19

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();
        }
    }
}

ClickOnce installs an Uninstall registry key in HKEY_CURRENT_USER which is accessible to your ClickOnce application.

The specific location is "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"

You will have to search for the key with the DisplayName of your Application.

You can then wrap the normal uninstall action,

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();
        }
    }
}
愿与i 2024-08-03 04:52:19

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"

找到 此处

There is no way to do that with ClickOnce itself, but you can create a standard Setup.exe bootstrapper that installs the ClickOnce application and which has a custom uninstall action.

Note that this however this creates two entries in the Add /Remove programs, so you need to hide one of the entries (the clickonce app).

Your final problem will then be that there is no "silent uninstall" option on clickonce, so you could do something like this:

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"

(Found here)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文