简体   繁体   English

以编程方式检查.NET Framework版本-我应该吗?

[英]Programatically check for version .NET Framework - Should I?

I got at task to extend en existing WinForm application to make a check weather or not the required .NET Framework (fx. 3.5) is installed. 我的任务是扩展现有的WinForm应用程序以进行检查,或者未安装所需的.NET Framework(fx。3.5)。

Well the issues is that - if there is no .NET Framework installed, the winform program is not able to run at all... I assume. 很好的问题是-如果未安装.NET Framework,则winform程序根本无法运行...我想。

I could (maybe) do like suggested here: (and make a c++ program that should start first, make the check and then launch the application) Check on .Net framework version from WinForms app But I would rather not go into c++. 我可以(也许)按照这里的建议进行操作:(并制作一个应该首先启动的c ++程序,进行检查,然后启动该应用程序) 从WinForms应用程序中检查.Net框架版本,但我不想进入c ++。

Another way seems to be this solution: Why isn't an exception thrown when the right .NET framework version is not present? 解决方案似乎是另一种方式: 当没有正确的.NET Framework版本时,为什么不引发异常? ... where you configure your application in app.config. ...在app.config中配置应用程序的位置。 But I doubt that will work if there i no .NET framework installed. 但是我怀疑如果没有安装.NET框架是否可以正常工作。

<startup>
    <supportedRuntime version="v3.5" />
</startup>

So my question is what is Best Practice in this area? 所以我的问题是这方面的最佳实践什么 Should I make the check one way or the other, or should I just make it a pre-requisite, that fx. 我应该以一种或另一种方式进行检查,还是仅作为前提条件,即fx。 .NET Framework version 3.5 is demanded? 需要.NET Framework 3.5版吗?

If the required framework is not installed, your application won't run, so checking if the framework is installed from within your app is checking something you already know to be true. 如果未安装所需的框架,则您的应用程序将无法运行,因此检查是否已从应用程序内部安装框架正在检查您已经知道是正确的事情。

If you want to check that the framework is installed, you really need to do this from within aa bootstrapper exe either written in a .NET version you know will exist (like .NET 2 as it gets installed on the machine with the OS) or some other language like C++. 如果要检查是否已安装框架,则确实需要从已知会存在的.NET版本(例如.NET 2,因为它随操作系统一起安装在计算机上)中编写的bootstrap exe文件中执行此操作,或者其他一些语言,例如C ++。

You can check in the registry (HKLM\\SOFTWARE\\Microsoft\\NET Framework Setup\\NDP) to see what frameworks are installed. 您可以签入注册表(HKLM \\ SOFTWARE \\ Microsoft \\ NET Framework Setup \\ NDP)以查看安装了哪些框架。 This can easily be done in C# or any other language. 这可以轻松地用C#或任何其他语言完成。

C# code something like this: C#代码如下所示:

var baseKeyName = @"SOFTWARE\Microsoft\NET Framework Setup\NDP";
var installedFrameworkVersions = Registry.LocalMachine.OpenSubKey(baseKeyName);

var versionNames = installedFrameworkVersions.GetSubKeyNames();

If you are deploying often for limited set of users, you should check out ClickOnce ( http://msdn.microsoft.com/en-us/library/t71a733d(v=vs.80).ASPX ) 如果您经常为有限的一组用户部署,则应查看ClickOnce( http://msdn.microsoft.com/zh-cn/library/t71a733d ( v=vs.80 ) .ASPX

It is a bit like Windows Installer, but simplified a lot. 它有点像Windows Installer,但简化了很多。 From the user perspective application will look like desktop icon. 从用户角度看,应用程序看起来像桌面图标。 When you click on the icon, it will automatically check all the requirements and install the latest version of your software if it has been updated. 当您单击该图标时,它将自动检查所有要求并安装最新版本的软件(如果已更新)。 This way you can ensure that users always has the required framework installed, as well users always will use the latest version. 这样,您可以确保用户始终安装了所需的框架,并且用户始终将使用最新版本。

要检查.net框架工作版本,请遵循http://support.microsoft.com/kb/318785

I got at task to extend en existing WinForm application to make a check weather or not the required .NET Framework (fx. 3.5) is installed 我的任务是扩展现有的WinForm应用程序以进行检查天气,或者未安装所需的.NET Framework(fx。3.5)

This doesn't makes sense, because it will not run if required component is missing. 这没有意义,因为如果缺少必需的组件,它将无法运行。

As already mentioned, typically installer is responsible for the job to check for components, interrupt installation, download and install them. 如前所述,通常安装程序负责检查组件,中断安装,下载并安装它们的工作。

But in any case, nothing prevents you to make bootstrapper , which will be able run in majority of times (aka required less/none components) and which will run your .net application . 但是无论如何,没有什么可以阻止您制作bootstrapper的 ,它可以在大多数情况下运行(aka所需的组件更少/无),并且可以运行.net应用程序 It can be organized in multiple ways: exe + exe (often renamed), exe + dll, exe + embedded resource (extract and run), etc... 它可以通过多种方式组织:exe + exe(通常重命名),exe + dll,exe +嵌入式资源(提取并运行)等。

If you think to use MS Visual C++ to make bootstrapper, then here is a bad new for you: it required component to be installed (yes, that's stupid, but it needs Microsoft Visual C++ 200x redistributable). 如果您认为使用MS Visual C ++来制作引导程序,那么这对您来说是一个不好的新消息:它需要安装组件 (是的,这很愚蠢,但是需要Microsoft Visual C ++ 200x可重新分发)。 So it would be again a job for installer, or you will have to learn how to write C/C++ pure WinAPI software, which is a pain, but doable. 因此,对于安装程序而言,这将再次成为工作,否则您将必须学习如何编写C / C ++纯WinAPI软件,这是一件痛苦的事,但可行。

To example, this is a check for .Net Framework 4.0 例如,这是对.Net Framework 4.0的检查

// check if net framework 4.0 or higher is installed
HKEY hkey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Net Framework Setup\\NDP\\v4", 0, KEY_READ, &hkey) != ERROR_SUCCESS)
    ; // some error
RegCloseKey(hkey);

Doesn't looks complicated? 看起来不复杂吗? Then go ahead! 那继续吧!

You are right about the program not running if the required .NET version is not installed. 如果未安装所需的.NET版本,则说明程序未运行是正确的。 You could use a bootstrapper, as others have said. 正如其他人所说,您可以使用引导程序。 You could simply let the application fail at runtime with the default message box that appears whining about the missing dependency. 您可以简单地让应用程序在运行时失败,并显示默认消息框,其中显示出缺少依赖项。

Another valid approach would be hosting the CLR yourself. 另一种有效的方法是自己托管CLR。 Here: 这里:

It's mostly like a bootstrapper too, but instead of launching the second (.NET-dependent) application it just loads it into the same process (if available). 它也几乎像一个引导程序,但是没有启动第二个(依赖于.NET的)应用程序,而是将其加载到同一进程中(如果可用)。

You could create a batch (.bat) file to verify the existence of the .NET Framework folders: 您可以创建一个批处理(.bat)文件来验证.NET Framework文件夹是否存在:

IF EXIST C:\\WINDOWS\\Microsoft.NET\\Framework[framework version here] (goto launchprogram) else goto errormessage 如果存在C:\\ WINDOWS \\ Microsoft.NET \\ Framework [此处为框架版本](转到启动程序),否则转到错误消息

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

相关问题 以编程方式检查安装了哪个版本的.NET Compact Framework - Programatically check which versions of .NET Compact Framework are installed 从WinForms应用程序检查.Net框架版本 - Check on .Net framework version from WinForms app 有没有一种简单的方法来检查 .NET Framework 版本? - Is there an easy way to check the .NET Framework version? COM-Interop:将.NET Framework版本从2.0更改为4.0 - 我是否也应该更改Guids? - COM-Interop: Change .NET Framework Version from 2.0 to 4.0 - Should I change the Guids also? 我应该使用类库来定位.NET Framework和C#版本? - What .NET Framework and C# version should I target with my class library? 如何使用可能不存在的.NET Framework版本安全地检查.NET Framework版本? - How to safely check .NET Framework version using a .NET Framework version that may not exist? 什么版本的 .Net 框架应该针对最终用户应用程序 - What version of .Net framework should be targeted for end user apps 我应该在C#程序中使用.NET Framework 3.5检测Windows 8.1的正确操作系统版本? - What should I do in my C# program to detect the correct OS version for Windows 8.1, using .NET Framework 3.5? 我应该在框架更新到新版本时重新发布我的项目吗? - Should I republish my project while the framework update to a new version? .NET Framework-降低版本 - .NET Framework - Lower the version
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM