简体   繁体   English

使用 C# 以编程方式卸载程序

[英]Programmatically uninstalling a program using C#

Earlier we released an application <application name> .早些时候我们发布了一个应用程序<application name> Later a new version of the application was released but with different application name <company name> <application name> .后来发布了应用程序的新版本,但具有不同的应用程序名称<company name> <application name> But both these applications put files in the same program folder.但是这两个应用程序都将文件放在同一个程序文件夹中。 But we have two different versions listed in uninstall programs.但是我们在卸载程序中列出了两个不同的版本。

Now that we want to release a newer version.现在我们要发布一个更新的版本。 An we want to uninstall the very first version from the user's pc.我们想从用户的电脑上卸载第一个版本。 how can I achieve this using c#.我如何使用 c# 实现这一点。 We are using windows 7 64 bit (application is 32 bit and installed in x86 folder).我们使用的是 Windows 7 64 位(应用程序是 32 位并安装在 x86 文件夹中)。

I don't have the application setup neither the application key我没有应用程序设置也没有应用程序密钥

If you have an installer project you can specify this in the installer properties.如果您有安装程序项目,您可以在安装程序属性中指定它。

See: VS2008 Setup Project: Uninstalling the previous MSI请参阅: VS2008 安装项目:卸载以前的 MSI

If you're application cannot detect it's early version (Different Name) ,than you probably should delete Data from Places where these 2 Application interferes .如果您的应用程序无法检测到它的早期版本(不同名称),那么您可能应该从这两个应用程序干扰的地方删除数据。 You could do that with File.Delete();你可以用File.Delete();做到这File.Delete(); but if these Files are somewhere where Application has not permission's you should force it run as Admin by Adding a Manifest File and replace that line.但是如果这些文件位于应用程序没有权限的地方,您应该通过添加清单文件并替换该行来强制它以管理员身份运行。

 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Have a look at the COM WindowsInstaller classes.查看 COM WindowsInstaller 类。 You will need to add a reference to the WindowsInstaller class to get access to it.您需要添加对 WindowsInstaller 类的引用才能访问它。 From here you can browse the MSI and perform actions on it if required.从这里您可以浏览 MSI 并根据需要对其执行操作。 I use this in my post build to modify the archive to fix a... undocumented functionality of Visual Studio.我在我的后期构建中使用它来修改存档以修复...未记录的 Visual Studio 功能。

// Create an Installer instance   
MsiInstaller = Activator.CreateInstance(Type.GetTypeFromProgID("WindowsInstaller.Installer")) as WindowsInstaller.Installer;
// Open the msi file for Read/Write   
MsiDatabase = MsiInstaller.OpenDatabase(MsiFile.FullName, MsiOpenDatabaseMode.msiOpenDatabaseModeTransact);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM