是否有用于验证安装的 .NET 版本的命令行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/492967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 11:07:16  来源:igfitidea点击:

Is there a command line command for verifying what version of .NET is installed

.netwindowsbatch-filecmd

提问by Amit Naidu

I have set of scripts for doing scripted installs. You can use the scripts on any server 2008 machine. However, I need to check if .NET 3.5 has been installed (before the scripts run) using a dos batch file. Is that possible?

我有一组用于执行脚本安装的脚本。您可以在任何 server 2008 机器上使用这些脚本。但是,我需要使用 dos 批处理文件检查 .NET 3.5 是否已安装(在脚本运行之前)。那可能吗?

I know I can check if a file in the C:\WINDOWS\Microsoft.NET\Framework\v3.5exists, but it would be nice to have something a little more reliable.

我知道我可以检查文件中的文件是否C:\WINDOWS\Microsoft.NET\Framework\v3.5存在,但是如果有一些更可靠的东西会很好。

I would like to check if it's actually installed, not just if the dir/file exists.

我想检查它是否实际安装,而不仅仅是目录/文件是否存在。

Thanks

谢谢

采纳答案by Andrew Hare

Unfortunately the best way would be to check for that directory. I am not sure what you mean but "actually installed" as .NET 3.5 uses the same CLR as .NET 3.0 and .NET 2.0 so all new functionality is wrapped up in new assemblies that live in that directory. Basically, if the directory is there then 3.5 is installed.

不幸的是,最好的方法是检查该目录。我不确定您的意思,但“实际安装”是因为 .NET 3.5 使用与 .NET 3.0 和 .NET 2.0 相同的 CLR,因此所有新功能都包含在该目录中的新程序集中。基本上,如果目录在那里,则安装 3.5。

Only thing I would add is to find the dir this way for maximum flexibility:

我唯一要添加的是以这种方式找到目录以获得最大的灵活性:

%windir%\Microsoft.NET\Framework\v3.5

回答by Amit Naidu

Since you said you want to know if its actually installed, I think the best way (short of running version specific code), is to check the reassuringly named "Install" registry key. 0x1 means yes:

既然你说你想知道它是否真的安装了,我认为最好的方法(缺少运行版本特定的代码)是检查令人放心的“安装”注册表项。0x1 表示是:

C:\>reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5"| findstr Install

   Install     REG_DWORD       0x1
   InstallPath REG_SZ  c:\WINNT\Microsoft.NET\Framework\v3.5\

This also happens to be the "Microsoft Recommended" official method.

这也恰好是“微软推荐”的官方方法

WMI is another possibility, but seems impractical (Slow? Takes 2 min on my C2D, SSD). Maybe it works better on your server:

WMI 是另一种可能性,但似乎不切实际(慢?在我的 C2D、SSD 上需要 2 分钟)。也许它在您的服务器上效果更好:

C:\>wmic product where "Name like 'Microsoft .Net%'" get Name, Version

Name                                                Version
Microsoft .NET Compact Framework 1.0 SP3 Developer  1.0.4292
Microsoft .NET Framework 3.0 Service Pack 2         3.2.30729
Microsoft .NET Framework 3.5 SP1                    3.5.30729
Microsoft .NET Compact Framework 2.0                2.0.5238
Microsoft .NET Framework 4 Client Profile           4.0.30319
Microsoft .NET Framework 4 Multi-Targeting Pack     4.0.30319
Microsoft .NET Framework 2.0 Service Pack 2         2.2.30729
Microsoft .NET Framework 1.1                        1.1.4322
Microsoft .NET Framework 4 Extended                 4.0.30319

C:\>wmic product where "name like 'Microsoft .N%' and version='3.5.30729'" get name

Name  
Microsoft .NET Framework 3.5 SP1

Other than these I think the only way to be 100% sure is to actually run a simple console app compiled targeting your framework version. Personally, I consider this unnecessary and trust the registry method just fine.

除了这些,我认为 100% 确定的唯一方法是实际运行一个针对您的框架版本编译的简单控制台应用程序。就个人而言,我认为这是不必要的,并且相信注册表方法就好了。

Finally, you could set up an intranet test site which is reachable from your server and sniffs the User Agent to determine .NET versions. But that's not a batch file solution of course. Also see doc here.

最后,您可以设置一个可从您的服务器访问的 Intranet 测试站点,并通过嗅探用户代理来确定 .NET 版本。但这当然不是批处理文件解决方案。另请参阅此处的文档

回答by MacG

You mean a DOS command such as below will do the job displaying installed .NET frameworks:

你的意思是一个像下面这样的 DOS 命令将完成显示已安装的 .NET 框架的工作:

wmic /namespace:\root\cimv2 path win32_product where "name like '%%.NET%%'" get version

The following may then be displayed:

然后可能会显示以下内容:

Version
4.0.30319

WMIC is quite useful once you master using it, much easier than coding WMI in scripts depending on what you want to achieve.

一旦您掌握了 WMIC 的使用方法,它就非常有用,这比根据您想要实现的目标在脚本中编写 WMI 容易得多。

回答by ajma

You can write yourself a little console app and use System.Environment.Version to find out the version. Scott Hanselman gives a blog postabout it.

您可以自己编写一个小控制台应用程序并使用 System.Environment.Version 找出版本。Scott Hanselman 发表了一篇关于它的博客文章

Or look in the registry for the installed versions. HKLM\Software\Microsoft\NETFramework Setup\NDP

或者在注册表中查找已安装的版本。HKLM\Software\Microsoft\NETFramework Setup\NDP

回答by ajma

If you're going to run a little console app, you may as well install clrver.exefrom the .NET SDK. I don't think you can get cleaner than that. This isn't my answer (but I happen to agree), I found it here.

如果您要运行一个小型控制台应用程序,您不妨clrver.exe从 .NET SDK安装。我认为你没有比这更干净的了。这不是我的答案(但我碰巧同意),我在这里找到了它。

回答by user3237950

REMSearch for the CONFIG file, if this doesn't exit then the user doesn't have the .Net framework 2.0 `

REM搜索 CONFIG 文件,如果没有退出,则用户没有 .Net framework 2.0 `

SET FileName=%windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG
IF EXIST %FileName% GOTO INSTALL_DIALER
ECHO.You currently do not have the Microsoft(c) .NET Framework 2.0 installed.

回答by JRobParsons

This is working for me:

这对我有用:

@echo off
SETLOCAL ENABLEEXTENSIONS

echo Verify .Net Framework Version

for /f "delims=" %%I in ('dir /B /A:D %windir%\Microsoft.NET\Framework') do (
    for /f "usebackq tokens=1,3 delims= " %%A in (`reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\%%I" 2^>nul ^| findstr Install`) do (
        if %%A==Install (
            if %%B==0x1 (
                echo %%I
            )
        )
    )
)

echo Do you see version v4.5.2 or greater in the list?

pause

ENDLOCAL 

The 2^>nulredirects errors to vapor.

2^>nul蒸气重定向错误。

回答by npocmaka

you can check installed c# compilers and the printed version of the .net:

您可以检查已安装的 c# 编译器和 .net 的打印版本:

@echo off

for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*csc.exe") do (
    set "l="
    for /f "skip=1 tokens=2 delims=k" %%$ in ('"%%# #"') do (
        if not defined l (
            echo Installed: %%$
            set l=%%$
        )
    )
)

echo latest installed .NET %l%

the csc.exedoes not have a -versionswitch but it prints the .net version in its logo. You can also try with msbuild.exe but .net framework 1.* does not have msbuild.

csc.exe没有一个-version开关,但它会在其标识的.NET版本。您也可以尝试使用 msbuild.exe,但 .net framework 1.* 没有 msbuild。

回答by Nolm? Informatique