简体   繁体   English

如何使用可能不存在的.NET Framework版本安全地检查.NET Framework版本?

[英]How to safely check .NET Framework version using a .NET Framework version that may not exist?

I have an application built with .NET 3.5. 我有一个使用.NET 3.5构建的应用程序。 If a user runs it without having .NET 3.5 installed, I want to have control of what they see and perhaps provide a message that they can understand (with a link to .NET 3.5) versus unhandled exception stack traces. 如果用户在没有安装.NET 3.5的情况下运行它,我希望能够控制他们看到的内容,并且可能提供他们可以理解的消息(带有指向.NET 3.5的链接)与未处理的异常堆栈跟踪。 But if they don't actually have .NET 3.5 how can my application control anything? 但如果他们实际上没有.NET 3.5我的应用程序如何控制任何东西呢?

Are you assuming that at least SOME version of .NET is installed? 您是否认为至少安装了某些版本的.NET?

You could write a small loader to be compatible with all versions to do the check. 您可以编写一个小型加载程序以与所有版本兼容以进行检查。

Or if .NET is not installed, found a simple batch program (though this could easily be done in C++ if you prefer). 或者如果没有安装.NET,找到一个简单的批处理程序(如果你愿意,可以用C ++轻松完成)。

@ECHO OFF
SET FileName=%windir%\Microsoft.NET\Framework\v1.1.4322
IF EXIST %FileName% GOTO Skip
ECHO.You currently do not have the Microsoft® .NET Framework 1.1 installed.
ECHO.This is required by the setup program for MyApplication.
ECHO.
ECHO.The Microsoft® .NET Framework 1.1 will now be installed on you system.
ECHO.After completion setup will continue to install MyApplication on your system.
ECHO.
Pause
SET FileName=
Start /WAIT .\dotnetfx.exe
Start .\Setup.exe
ECHO ON
Exit
Tongue Tiedkip
SET FileName=
Start .\Setup.exe
ECHO ON
Exit

This might also help. 也可能有所帮助。

You could instead consider using an installer that requires 3.5 is installed. 您可以考虑使用需要安装3.5的安装程序。 That would prevent the app from getting to the user in the first place if they can't run it. 如果他们无法运行,这将阻止应用程序首先访问用户。

Better yet, you could use an installer technology, such as Wix or a Visual Stuido setup project, that checks during installation that all the prerequisites for you software, such as a particular .NET framework runtime exist. 更好的是,您可以使用安装程序技术,例如Wix或Visual Stuido安装项目,它在安装期间检查您的软件的所有先决条件,例如特定的.NET框架运行时。 If not, it will install them, or at least tell the user where to get them and what to do next 如果没有,它将安装它们,或者至少告诉用户在哪里获取它们以及下一步该做什么

You need an non .NET EXE file that checks for the .NET runtimes. 您需要一个非.NET EXE文件来检查.NET运行时。 It could be written in anything that you know exists in the target PC. 它可以写在您知道目标PC中存在的任何内容中。 For example, if targeting Windows 2000 or above, you know the Visual Basic 6.0 runtime is present, so your installer could always start a Visual Basic 6.0 splash application that checks for the runtimes before starting your .NET EXE file. 例如,如果以Windows 2000或更高版本为目标,您就知道Visual Basic 6.0运行时存在,因此您的安装程序始终可以启动Visual Basic 6.0初始应用程序,该应用程序在启动.NET EXE文件之前检查运行时。 A better way would be to write a C++ application that didn't rely on any runtime other than the Windows APIs. 更好的方法是编写一个不依赖于Windows API之外的任何运行时的C ++应用程序。

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

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