什么是万无一失的方法来判断哪个版本的.NET 安装在生产 Windows 服务器上?

与其说这个问题与编程有关,不如说它与部署有关。

我发现自己与公司里的一个小组交谈了很多,他们的工作是维护我们的生产 Windows 服务器并在其上部署我们的代码。由于法律和法规遵循的原因,我没有直接的可见性或对服务器的任何控制,所以我可以告诉哪个版本(s)的。NET 是安装在任何一个是通过指示,我给该组。

到目前为止,我能想到的所有判断安装了哪个版本的方法(检查匹配1.1或2.0的管理工具,检查“添加/删除程序”列表中的条目,检查 c: Windows Microsoft.NET 下是否存在目录)都是有缺陷的(我见过至少有一台机器在管理工具下安装了2.0但没有2.0条目——这个方法没有告诉你关于3.0 + 的任何信息,“添加/删除程序”列表可能与现实不同步,目录的存在并不一定意味着什么)。

考虑到我通常需要提前知道这些东西已经到位(发现“哎呀,这个版本没有你需要的所有版本和服务包”在短维护窗口中并不能很好地工作) ,而且我不得不通过代理进行检查,因为我不能直接登录服务器,那么什么是最简单的方法来判断哪个版本的。NET 是否安装在生产 Windows 服务器上?最好是使用框架安装的内部方法,因为它会更快,不需要加载某种实用程序,而且如果框架没有正确安装,但仍然有文件到位(例如,有一个目录和 gacutil.exe 的内嵌,但该版本的框架并没有真正“安装”) ,这种方法肯定会失败

编辑: 在没有一个内置在框架中的简单易用的方法的情况下,有人知道一个好的、轻量级的、不需要安装的程序可以找出这个问题吗?我可以想象有人可以很容易地写一个,但如果一个已经存在,那将是更好的。

113888 次浏览

OneTouch deployment will do all the detection and installation of pre-requisites. It's probably best to go with a pre-made solution than trying to roll your own. Trying to roll your own may lead to problems because whatever thing you key on may change with a hotfix or service pack. Likely Microsoft has some heuristic for determining what version is running.

It is probably a nasty way to find versions out, but I was always under the impression that all version got installed to <root>:\WINDOWS\Microsoft.NET\Framework.

This provides folders with names such as v2.0.50727 which I believe give detailed version information.

You can programmatically check the registry and a few other things as per this blog entry.

The registry key to look at is

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\...]

You should open up IE on the server for which you are looking for this info, and go to this site: http://www.hanselman.com/smallestdotnet/

That's all it takes.

The site has a script that looks your browser's "UserAgent" and figures out what version (if any) of the .NET Framework you have (or don't have) installed, and displays it automatically (then calculates the total size if you chose to download the .NET Framework).

The official Microsoft answer on how to do this is in KB article 318785.

The Microsoft way is this:

MSDN: How to determine Which .NET Framework Versions Are Installed (which directs you to the following registry key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\...)

If you want foolproof that's another thing. I wouldn't worry about an xcopy of the framework folder. If someone did that I would consider the computer broken.

The most foolproof way would be to write a small program that uses each version of .NET and the libraries that you care about and run them.

For a no install method, PowerBasic is an excellent tool. It creates small no runtime required exe's. It could automate the checks described in the MS KB article above.

The official way to detect .NET 3.0 is described here

http://msdn.microsoft.com/en-us/library/aa480198.aspx

Flawed, because it requires the caller to have registry access permissions.

MSDN also mentions a technique for detecting .NET 3.5 by checking the User Agent string:

http://msdn.microsoft.com/en-us/library/bb909885.aspx

I think Microsoft should have done a better job than this.

If the machine that you want to check has the .NET SDK installed, you can use a SDK command prompt and run the program CLRVer.exe.

Well, like Dean said, you can look at the registry and do what he did. To check if he really has CLR .NET Framework installed, you should look for the MSCorEE.dll file in the %SystemRoot%\System32 directory.

As per CodeTrawler's answer, the solution is to enter the following into an explorer window:

%systemroot%\Microsoft.NET\Framework

Then search for:

Mscorlib.dll

...and right-click / go to the version tab for each result.

Strangely enough, I wrote some code to do this back when 1.1 came out (what was that, seven years ago?) and tweaked it a little when 2.0 came out. I haven't looked at it in years as we no longer manage our servers.

It's not foolproof, but I'm posting it anyway because I find it humorous; in that it's easier to do in .NET and easier still in power shell.

bool GetFileVersion(LPCTSTR filename,WORD *majorPart,WORD *minorPart,WORD *buildPart,WORD *privatePart)
{
DWORD dwHandle;
DWORD dwLen = GetFileVersionInfoSize(filename,&dwHandle);
if (dwLen) {
LPBYTE lpData = new BYTE[dwLen];
if (lpData) {
if (GetFileVersionInfo(filename,0,dwLen,lpData)) {
UINT uLen;
VS_FIXEDFILEINFO *lpBuffer;
VerQueryValue(lpData,_T("\\"),(LPVOID*)&lpBuffer,&uLen);
*majorPart = HIWORD(lpBuffer->dwFileVersionMS);
*minorPart = LOWORD(lpBuffer->dwFileVersionMS);
*buildPart = HIWORD(lpBuffer->dwFileVersionLS);
*privatePart = LOWORD(lpBuffer->dwFileVersionLS);
delete[] lpData;
return true;
}
}
}
return false;
}


int _tmain(int argc,_TCHAR* argv[])
{
_TCHAR filename[MAX_PATH];
_TCHAR frameworkroot[MAX_PATH];
if (!GetEnvironmentVariable(_T("systemroot"),frameworkroot,MAX_PATH))
return 1;
_tcscat_s(frameworkroot,_T("\\Microsoft.NET\\Framework\\*"));
WIN32_FIND_DATA FindFileData;
HANDLE hFind = FindFirstFile(frameworkroot,&FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
return 2;
do {
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
_tcslen(FindFileData.cAlternateFileName) != 0) {
_tcsncpy_s(filename,frameworkroot,_tcslen(frameworkroot)-1);
filename[_tcslen(frameworkroot)] = 0;
_tcscat_s(filename,FindFileData.cFileName);
_tcscat_s(filename,_T("\\mscorlib.dll"));
WORD majorPart,minorPart,buildPart,privatePart;
if (GetFileVersion(filename,&majorPart,&minorPart,&buildPart,&privatePart )) {
_tprintf(_T("%d.%d.%d.%d\r\n"),majorPart,minorPart,buildPart,privatePart);
}
}
} while (FindNextFile(hFind,&FindFileData) != 0);
FindClose(hFind);
return 0;
}

Also, see the Stack Overflow question How to detect what .NET Framework versions and service packs are installed? which also mentions:

There is an official Microsoft answer to this question at the knowledge base article [How to determine which versions and service pack levels of the Microsoft .NET Framework are installed][2]

Article ID: 318785 - Last Review: November 7, 2008 - Revision: 20.1 How to determine which versions of the .NET Framework are installed and whether service packs have been applied.

Unfortunately, it doesn't appear to work, because the mscorlib.dll version in the 2.0 directory has a 2.0 version, and there is no mscorlib.dll version in either the 3.0 or 3.5 directories even though 3.5 SP1 is installed ... Why would the official Microsoft answer be so misinformed?

http://www.asoft.be/prod_netver.html

Use this "good, lightweight, no-install-required program"

Found answer from here:

Check which .NET Framework version is installed

Open Command Prompt and copy paste one of the below command lines

dir %WINDIR%\Microsoft.Net\Framework\v*

or

dir %WINDIR%\Microsoft.Net\Framework\v* /O:-N /B

If you want to find versions prior to .NET 4.5, use code for a console application. Like this:

using System;
using System.Security.Permissions;
using Microsoft.Win32;


namespace findNetVersion
{
class Program
{
static void Main(string[] args)
{
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
{
foreach (string versionKeyName in ndpKey.GetSubKeyNames())
{
if (versionKeyName.StartsWith("v"))
{


RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
string name = (string)versionKey.GetValue("Version", "");
string sp = versionKey.GetValue("SP", "").ToString();
string install = versionKey.GetValue("Install", "").ToString();
if (install == "") //no install info, must be later version
Console.WriteLine(versionKeyName + "  " + name);
else
{
if (sp != "" && install == "1")
{
Console.WriteLine(versionKeyName + "  " + name + "  SP" + sp);
}
}
if (name != "")
{
continue;
}
foreach (string subKeyName in versionKey.GetSubKeyNames())
{
RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
name = (string)subKey.GetValue("Version", "");
if (name != "")
sp = subKey.GetValue("SP", "").ToString();
install = subKey.GetValue("Install", "").ToString();
if (install == "") //no install info, ust be later
Console.WriteLine(versionKeyName + "  " + name);
else
{
if (sp != "" && install == "1")
{
Console.WriteLine("  " + subKeyName + "  " + name + "  SP" + sp);
}
else if (install == "1")
{
Console.WriteLine("  " + subKeyName + "  " + name);
}
}
}
}
}
}
}
}
}

Otherwise you can find .NET 4.5 or later by querying like this:

private static void Get45or451FromRegistry()
{
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\"))
{
int releaseKey = (int)ndpKey.GetValue("Release");
{
if (releaseKey == 378389)


Console.WriteLine("The .NET Framework version 4.5 is installed");


if (releaseKey == 378758)


Console.WriteLine("The .NET Framework version 4.5.1  is installed");


}
}
}

Then the console result will tell you which versions are installed and available for use with your deployments. This code come in handy, too because you have them as saved solutions for anytime you want to check it in the future.

To determine your server's support for .NET Framework 4.5 and later versions (tested through 4.5.2): If you don't have Registry access on the server, but have app publish rights to that server, create an MVC 5 app with a trivial controller, like this:

using System.Web.Mvc;


namespace DotnetVersionTest.Controllers
{
public class DefaultController : Controller
{
public string Index()
{
return "simple .NET version test...";
}
}
}

Then in your Web.config, walk through the desired .NET Framework versions in the following section, changing the targetFramework values as desired:

<system.web>
<customErrors mode="Off"/>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
</system.web>

Publish each target to your server, then browse to <app deploy URL>/Default. If your server supports the target framework, then the simple string will display from your trivial Controller. If not, you'll receive an error like the following:

Example of unsupported .NET 4.5.2 on server

So in this case, my target server doesn't yet support .NET Framework 4.5.2.

I went into Windows Update & looked at the update history, knowing the server patching is kept up-to-date. I scanned down for .NET updates and it showed me exactly which versions had had updates, which allowed me to conclude which versions were installed.

To get the installed dotnet version,
Create a Console app. Add this class Run that

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication2
{
public class GetDotNetVersion
{
public static void Get45PlusFromRegistry()
{
const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
{
if (ndpKey != null && ndpKey.GetValue("Release") != null)
{
Console.WriteLine(".NET Framework Version: " + CheckFor45PlusVersion((int)ndpKey.GetValue("Release")));
}
else
{
Console.WriteLine(".NET Framework Version 4.5 or later is not detected.");
}
}
}


// Checking the version using >= will enable forward compatibility.
private static string CheckFor45PlusVersion(int releaseKey)
{
if (releaseKey >= 394802)
return "4.6.2 or later";
if (releaseKey >= 394254)
{
return "4.6.1";
}
if (releaseKey >= 393295)
{
return "4.6";
}
if ((releaseKey >= 379893))
{
return "4.5.2";
}
if ((releaseKey >= 378675))
{
return "4.5.1";
}
if ((releaseKey >= 378389))
{
return "4.5";
}
// This code should never execute. A non-null release key shoul
// that 4.5 or later is installed.
return "No 4.5 or later version detected";
}
}
// Calling the GetDotNetVersion.Get45PlusFromRegistry method produces
// output like the following:
//       .NET Framework Version: 4.6.1
}

I found this one quite useful. here's the source

enter image description here