C# 检查应用程序是否已经安装

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/211192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:15:40  来源:igfitidea点击:

Check if the application is already installed

c#

提问by

I want to check if an app is alredy installed through launch conditions.If yes i want to exit the installer,if no the installation should continue.Could anyone tell how to achive this in c# installer?.

我想检查一个应用程序是否已经通过启动条件安装。如果是,我想退出安装程序,如果没有,安装应该继续。有人能告诉我如何在 c# 安装程序中实现这个吗?。

Regards, Harsh Suman

问候,严厉的苏曼

回答by Claudiu

Put an entry in the registry on install. If, when the installer runs, the entry is there, then the program is already installed (or your user has hacked the registry to make the installer think it has been). If it's not, then you haven't installed it yet.

在安装时在注册表中放置一个条目。如果,当安装程序运行时,条目在那里,那么该程序已经安装(或者您的用户已经入侵了注册表以使安装程序认为它已经安装)。如果不是,那么你还没有安装它。

回答by Jonathan Wright

I'm not sure on how to do this specifically with C#, but while using the Nullsoft Installerthis is the approach I've seen used: before installing, check for a registry key that is created during the install.

我不确定如何专门使用 C# 执行此操作,但是在使用Nullsoft 安装程序时,这是我见过的方法:在安装之前,检查在安装过程中创建的注册表项。

Uninstallers show up in the Add/Remove Programs control panel. The list of the uninstallers is stored under HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall. If the installer registers an uninstaller, the uninstaller's key is a good key for the installer to check for, because:

卸载程序显示在“添加/删除程序”控制面板中。卸载程序列表存储在 HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall 下。如果安装程序注册了卸载程序,则卸载程序的密钥是安装程序检查的好密钥,因为:

  1. Assuming the program is being installed for all users, the key tested for must be under HKey Local Machine (HKLM) not a key specific to the current user (HKey Current User). The uninstaller's key is under HKLM

  2. Sometimes multiple versions of the same program need to be installed side-by-side. Uninstaller keys should be specific to each version, so rules can be added to check the installer's version number against installed versions.

  1. 假设为所有用户安装程序,测试的密钥必须在 HKey 本地机器 (HKLM) 下,而不是特定于当前用户 (HKey Current User) 的密钥。卸载程序的密钥在 HKLM 下

  2. 有时需要并行安装同一程序的多个版本。卸载程序密钥应特定于每个版本,因此可以添加规则以根据已安装的版本检查安装程序的版本号。

There is a limitation with checking for registry keys to tell if a program is installed: if the program is deleted out of Program Files by hand (without using the uninstaller), reinstalling will fail. To avoid this problem, after finding the uninstaller registry key, the installer can check that the uninstaller program still exists. If it does, it's probably safe to assume the program is still installed.

检查注册表项以判断程序是否已安装存在一个限制:如果手动将程序从 Program Files 中删除(不使用卸载程序),则重新安装将失败。为避免此问题,在找到卸载程序注册表项后,安装程序可以检查卸载程序是否仍然存在。如果是这样,则可以安全地假设该程序仍处于安装状态。

While playing with installers and conditional installation, it's worth keeping in mind that sometimes reinstalling is useful for cleaning up problems. It can be tedious to be forced to walk through a (troublesome) uninstall to be able to reinstall.

在使用安装程序和有条件安装时,值得记住的是,有时重新安装对于清理问题很有用。被迫完成(麻烦的)卸载才能重新安装可能会很乏味。

See NSIS's page on Add/Remove Programsfor more details on uninstall registry keys.

有关卸载注册表项的更多详细信息,请参阅NSIS 上添加/删除程序的页面

回答by Mike Hall

If you don't want to put anything in the registry (due to admin rights or anything like that), you can simply check to see if the folders or files you install are present. Yes, this assumes you install to the same folder every time, but it's an option.

如果您不想在注册表中放置任何内容(由于管理员权限或类似原因),您可以简单地检查一下您安装的文件夹或文件是否存在。是的,这假设您每次都安装到同一个文件夹,但这是一个选项。