[英]c# Visual Studio 2015 - how to create a Setup which uninstall other application
Initially I created an application that I completely rewrite in a second version. 最初,我创建了一个应用程序,并在第二个版本中将其完全重写。 It is a complete different Visual studio solution. 这是一个完整的Visual Studio解决方案。 Now I would like that its Setup installer uninstall the previous version but because it was not created using the same solution, the automatic uninstallation of previous version does not work. 现在,我希望其安装程序安装程序可以卸载以前的版本,但是由于它不是使用相同的解决方案创建的,因此无法自动卸载以前的版本。
Is there any way to force the installer to uninstall certain application based on product name or product code? 有什么方法可以强制安装程序根据产品名称或产品代码来卸载某些应用程序?
I found a WMIC command that works when run from command line 我发现从命令行运行时可以工作的WMIC命令
wmic product where name="XXXX" call uninstall /nointeractive
So I created a VBS script which execute a bat file containing the WMIC code and I added it to the Setup project 因此,我创建了一个VBS脚本,该脚本执行包含WMIC代码的bat文件,并将其添加到Setup项目中
dim shell
set shell=createobject("wscript.shell")
shell.run "uninstallAll.bat",0,true
set shell=nothing
but when I run the result MSI, it fires an error 1001, meaning that a service already exists. 但是当我运行结果MSI时,它会引发错误1001,表示服务已经存在。 , in other words the uninstallation didn't work. ,也就是说,卸载无效。 The old program is still present and they create a service with the same name. 旧程序仍然存在,并且它们创建具有相同名称的服务。 :/ :/
any suggestion? 有什么建议吗?
There are 2 options: 有2个选项:
You can increase the version of MSI project so it will treat as upgrade and it will not throw any error while installing. 您可以增加MSI项目的版本,以便将其视为升级,并且在安装时不会引发任何错误。
another way out is the write some in the installer project as follows: 另一种解决方法是在安装程序项目中编写一些内容,如下所示:
protected override void OnBeforeInstall(IDictionary savedState) { //Write uninstall powershell script //installutil /u <yourproject>.exe using (PowerShell PowerShellInstance = PowerShell.Create()) { PowerShellInstance.AddScript(""); PowerShellInstance.AddParameter(""); } PowerShellInstance.Invoke(); }
Note: This InstallUtil is available with the .NET Framework, and its path is %WINDIR%\\Microsoft.NET\\Framework[64]\\<framework_version>
. 注意:此InstallUtil与.NET Framework一起提供,其路径为%WINDIR%\\Microsoft.NET\\Framework[64]\\<framework_version>
。
For example, for the 32-bit version of the .NET Framework 4 or 4.5.*, if your Windows installation directory is C:\\Windows
, the path is C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\InstallUtil.exe
. 例如,对于32位版本的.NET Framework 4或4.5。*,如果Windows安装目录为C:\\Windows
,则路径为C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\InstallUtil.exe
。
For the 64-bit version of the .NET Framework 4 or 4.5.*, the default path is C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\InstallUtil.exe
对于.NET Framework 4或4.5。*的64位版本,默认路径为C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\InstallUtil.exe
I decided to go for the option of introducing c# code in the project installer. 我决定选择在项目安装程序中引入c#代码。 Firstly I added the reference for System.Management.Automation via nuget 首先,我通过nuget添加了System.Management.Automation的引用。
https://www.nuget.org/packages/System.Management.Automation https://www.nuget.org/packages/System.Management.Automation
After this, I just created a string variable containing the PS code I need to uninstall several programs with a similar name. 之后,我刚刚创建了一个包含PS代码的字符串变量,我需要卸载几个具有类似名称的程序。
string unInstallKiosk = @"$app = get-WMIObject win32_Product -Filter ""name like 'KIOSK'""
foreach ($program in $app){
$app2 = Get-WmiObject -Class Win32_Product | Where -Object { $_.IdentifyingNumber -match ""$($program.identifyingNumber)""
}
$app2.Uninstall()}";
and passed this variable to the method PowerShellInstance.AddScript() 并将此变量传递给方法PowerShellInstance.AddScript()
PowerShellInstance.AddScript(unInstallKiosk);
The installation ends but the uninstallation simply dont happens. 安装结束,但是卸载根本不会发生。
anyone has an idea how to solve this? 任何人有一个想法如何解决这个问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.