English 中文(简体)
以静默方式卸载ClickOnce应用程序
原标题:Uninstalling a ClickOnce application silently

我们有一个生产应用程序,它是使用Visual Studio内置的ClickOnce部署工具部署的。我正在编写一个批处理文件以卸载应用程序:

rundll32.exe dfshim.dll,ShArpMaintain AppName.application, Culture=neutral,
PublicKeyToken=XXXXXX, processorArchitecture=x86

批处理文件起作用,并调用应用程序的卸载。然而,我希望默默地做这件事。我试过/Q/Q/S/Silent,但没有任何乐趣。

我该怎么做?


我不想隐藏批处理文件窗口。只有ClickOnce窗口。

最佳回答

由于似乎没有很好的解决方案,我实现了一个新的ClickOnce卸载程序。它可以通过命令行从.NET调用,也可以作为自定义操作集成到WiX安装项目中。

https://github.com/6wunderkinder/Wunder.ClickOnceUninstaller

我们在Wunderlist 2.1版本中使用了这一点,从ClickOnce切换到了Windows安装程序包。它集成到安装过程中,对用户完全透明。

问题回答

我可以确认WMIC不适用于ClickOnce应用程序。它们只是没有在其中列出。。。

我想把这个放在这里,因为我已经研究了很长一段时间,无法找到一个完整的解决方案。

我对整个编程工作还很陌生,但我认为这可以为如何进行提供一些想法。

它基本上验证应用程序当前是否正在运行,如果是,它会杀死它。然后它检查注册表以找到卸载字符串,将其放入批处理文件中,然后等待进程结束。然后它执行Sendkeys以自动同意卸载。就是这样。

namespace MyNameSpace
{
    public class uninstallclickonce
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]

        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [System.Runtime.InteropServices.DllImport("user32.dll")]

        private static extern bool SetForegroundWindow(IntPtr hWnd);

        private Process process;
        private ProcessStartInfo startInfo;

        public void isAppRunning()
        {
            // Run the below command in CMD to find the name of the process
            // in the text file.
            //
            //     WMIC /OUTPUT:C:ProcessList.txt PROCESS get Caption,Commandline,Processid
            //
            // Change the name of the process to kill
            string processNameToKill = "Auto-Crop"; 

            Process [] runningProcesses = Process.GetProcesses();

            foreach (Process myProcess in runningProcesses)
            {
                // Check if given process name is running
                if (myProcess.ProcessName == processNameToKill)
                {
                    killAppRunning(myProcess);
                }
            }
        }

        private void killAppRunning(Process myProcess)
        {
            // Ask the user if he wants to kill the process
            // now or cancel the installation altogether
            DialogResult killMsgBox =
                MessageBox.Show(
                    "Crop-Me for OCA must not be running in order to get the new version
If you are ready to close the app, click OK.
Click Cancel to abort the installation.",
                    "Crop-Me Still Running",
                    MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Question);

            switch(killMsgBox)
            {
                case DialogResult.OK:
                    //Kill the process
                    myProcess.Kill();
                    findRegistryClickOnce();
                    break;
                case DialogResult.Cancel:
                    //Cancel whole installation
                    break;
            }
        }

        private void findRegistryClickOnce()
        {
            string uninstallRegString = null; // Will be ClickOnce Uninstall String
            string valueToFind = "Crop Me for OCA"; // Name of the application we want
                                                    // to uninstall (found in registry)
            string keyNameToFind = "DisplayName"; // Name of the Value in registry
            string uninstallValueName = "UninstallString"; // Name of the uninstall string

            //Registry location where we find all installed ClickOnce applications
            string regProgsLocation = 
                "Software\Microsoft\Windows\CurrentVersion\Uninstall";

            using (RegistryKey baseLocRegKey = Registry.CurrentUser.OpenSubKey(regProgsLocation))
            {
                //Console.WriteLine("There are {0} subkeys in here", baseLocRegKey.SubKeyCount.ToString());

                foreach (string subkeyfirstlevel in baseLocRegKey.GetSubKeyNames())
                {
                   //Can be used to see what you find in registry
                   // Console.WriteLine("{0,-8}: {1}", subkeyfirstlevel, baseLocRegKey.GetValueNames());

                    try
                    {
                        string subtest = baseLocRegKey.ToString() + "\" + subkeyfirstlevel.ToString();

                        using (RegistryKey cropMeLocRegKey =
                                 Registry.CurrentUser.OpenSubKey(regProgsLocation + "\" + subkeyfirstlevel))
                        {
                            //Can be used to see what you find in registry
                            //  Console.WriteLine("Subkey DisplayName: " + cropMeLocRegKey.GetValueNames());

                            //For each
                            foreach (string subkeysecondlevel in cropMeLocRegKey.GetValueNames())
                            {
                                // If the Value Name equals the name application to uninstall
                                if (cropMeLocRegKey.GetValue(keyNameToFind).ToString() == valueToFind)
                                {
                                    uninstallRegString = cropMeLocRegKey.GetValue(uninstallValueName).ToString();

                                    //Exit Foreach
                                    break;
                                }
                            }
                        }
                    }
                    catch (System.Security.SecurityException)
                    {
                        MessageBox.Show("security exception?");
                    }
                }
            }
            if (uninstallRegString != null)
            {
                batFileCreateStartProcess(uninstallRegString);
            }
        }

        // Creates batch file to run the uninstall from
        private void batFileCreateStartProcess(string uninstallRegstring)
        {
            //Batch file name, which will be created in Window s temps foler
            string tempPathfile = Path.GetTempPath() + "cropmeuninstall.bat";

            if (!File.Exists(@tempPathfile))
            {
                using (FileStream createfile = File.Create(@tempPathfile))
                {
                    createfile.Close();
                }
            }

            using (StreamWriter writefile = new StreamWriter(@tempPathfile))
            {
                //Writes our uninstall value found earlier in batch file
                writefile.WriteLine(@"Start " + uninstallRegstring);
            }

            process = new Process();
            startInfo = new ProcessStartInfo();

            startInfo.FileName = tempPathfile;
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit();

            File.Delete(tempPathfile); //Deletes the file

            removeClickOnceAuto();
        }

        // Automation of clicks in the uninstall to remove the
        // need of any user interactions
        private void removeClickOnceAuto()
        {
            IntPtr myWindowHandle = IntPtr.Zero;

            for (int i = 0; i < 60 && myWindowHandle == IntPtr.Zero; i++)
            {
                Thread.Sleep(1500);

                myWindowHandle = FindWindow(null, "Crop Me for OCA Maintenance");
            }

            if (myWindowHandle != IntPtr.Zero)
            {
                SetForegroundWindow(myWindowHandle);

                SendKeys.Send("+{TAB}"); // Shift + TAB
                SendKeys.Send("{ENTER}");
                SendKeys.Flush();
            }
        }
    }
}

您可以尝试使用隐藏开始

不能取消ClickOnce应用程序的卸载对话框。你可以写一个小.NET应用程序来卸载ClickOnce应用程序,并以编程方式点击对话框上的按钮,这样用户就不需要执行任何操作。这是你能做的最好的事情。





相关问题
complex batch replacement linux

I m trying to do some batch replacement for/with a fairly complex pattern So far I find the pattern as: find ( -name *.php -o -name *.html ) -exec grep -i -n hello {} + The string I want ...

How to split a string by spaces in a Windows batch file?

Suppose I have a string "AAA BBB CCC DDD EEE FFF". How can I split the string and retrieve the nth substring, in a batch file? The equivalent in C# would be "AAA BBB CCC DDD EEE FFF".Split()[n]

How to check which Operating System?

How can I check OS version in a batch file or through a vbs in an Windows 2k/2k3 environment ? You know ... Something like ... : "If winver Win2k then ... or if winver Win2k3 then ....

热门标签