Programmatically check the version of the .NET Framework. Should I?

I have the task of expanding an existing WinForm application to do a weather check or if the installed .NET Framework (fx. 3.5) is not required.

Well, the problem is that if the .NET Framework is not installed, the winform program will not be able to run at all ... I guess.

I could (maybe) do something suggested here: (and create a C ++ program that should start first, check, and then launch the application) Check the version of the .Net framework from the WinForms application But I would prefer not to go in C ++.

Another solution would be: Why is there no exception if the correct version of the .NET Framework is missing? ... where you configure your application in app.config. But I doubt that this will work if I did not install the .NET framework.

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

So my question is: what is Best Practice in this area? Should I do the check anyway, or should I just make it a precondition that fx. Requires .NET Framework version 3.5?

+6
source share
6 answers

If the required infrastructure is not installed, your application will not start, so check if the framework is installed from your application, check that you already know to be true.

If you want to verify that the infrastructure is installed, you really need to do this from aa bootstrapper exe written in the .NET version that you know will exist (for example, .NET 2, since it is installed on the computer using the OS) or which or another language, for example C ++.

You can check in the registry (HKLM \ SOFTWARE \ Microsoft \ NET Framework Setup \ NDP) to find out which frameworks are installed. This is easy to do in C # or any other language.

C # code is something like this:

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

If you often deploy a limited set of users, you should check out ClickOnce ( http://msdn.microsoft.com/en-us/library/t71a733d(v=vs .80) .ASPX )

This is a bit like the Windows Installer, but simplified. From the user's point of view, the application will look like a 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. Thus, you can guarantee that users always have the necessary infrastructure, and users will always use the latest version.

0
source

To check the working version of the .net frame, follow http://support.microsoft.com/kb/318785

0
source

I have the task of expanding an existing WinForm application to check if the .NET Framework (fx. 3.5) is installed or not.

This does not make sense because it will not work if the required component is missing.

As already mentioned, usually the installer is responsible for the task, checking for the presence of components, interrupting installation, downloading, and installation.

But in any case, nothing prevents you from making a bootloader that will work in most cases (aka requires fewer / no components) and which will launch your .net application. It can be organized in several ways: exe + exe (often renamed), exe + dll, exe + built-in resource (extract and run), etc.

If you are thinking of using MS Visual C ++ to create a bootstrap, this is bad news for you: you need to install the component (yes, this is stupid, but it requires Microsoft Visual C ++ 200x redistributable). Thus, it will again be work for the installer, or you will need to learn to write C / C ++ pure WinAPI software, which is sick but doable.

For example, this is a check on .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 look complicated? Then go ahead!

0
source

You are right that the program does not run if the required version of .NET is not installed. You can use the bootloader as others have said. You can simply let the application crash at runtime with the default message field that appears in anticipation of a missing dependency.

Another good approach is to host the CLR yourself. Here:

This is basically the same as bootstrapper, but instead of launching a second (.NET dependent) application, it just loads it into the same process (if available).

0
source

You can create a batch (.bat) file to check for .NET Framework folders:

IF EXIST C: \ WINDOWS \ Microsoft.NET \ Framework [framework version here] (goto launchprogram) else goto errormessage

0
source

All Articles