简体   繁体   中英

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. 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. But if they don't actually have .NET 3.5 how can my application control anything?

Are you assuming that at least SOME version of .NET is installed?

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).

@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. 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. 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. It could be written in anything that you know exists in the target 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. A better way would be to write a C++ application that didn't rely on any runtime other than the Windows APIs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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