[英]Way to determine framework version without Registry
I've searching a long time, but i couldn't find answer. 我搜索了很长时间,但我找不到答案。 Is there any way to determine framework and service pack .NET installed on PC, without access to registry in C#?
有没有办法确定PC上安装的框架和Service Pack .NET,而无需访问C#中的注册表? I can use registry keys, but i have to do this without access to registry.
我可以使用注册表项,但我必须这样做而无需访问注册表。 I read something about directories in C:\\Windows\\Microsoft .NET, but there, I only found framework version, nothing about SP.
我在C:\\ Windows \\ Microsoft .NET中读到了一些关于目录的内容,但在那里,我只发现了框架版本,没有关于SP的内容。 But I need framework and Service Pack.
但我需要框架和Service Pack。 Can somebody help me?
有人能帮助我吗?
Regards, Greg 问候,格雷格
string clrVersion = System.Environment.Version.ToString();
string dotNetVersion = Assembly
.GetExecutingAssembly()
.GetReferencedAssemblies()
.Where(x => x.Name == "mscorlib").First().Version.ToString();
you could use WMI to get a list of all the installed software filtering the result to achieve your goal 您可以使用WMI获取所有已安装软件的列表,过滤结果以实现您的目标
public static class MyClass
{
public static void Main()
{
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
foreach (ManagementObject mo in mos.Get())
{
Console.WriteLine(mo["Name"]);
}
}
}
I think it's possible to ask the WMI. 我认为有可能问WMI。
Query for all Win32_Product elements and look for the Name Property contains "Microsoft .NET Framework" 查询所有Win32_Product元素并查找Name属性包含“Microsoft .NET Framework”
ServicePack information is also provided by WMI.. But I don't know exactly where. WMI也提供ServicePack信息..但我不确切知道在哪里。
You can do this: 你可以这样做:
System.Environment.Version.ToString()
( CLR Version only ) ( 仅限CLR版 )
or this 或这个
from MSDN blog on Updated sample .NET Framework detection code that does more in-depth checking 来自MSDN博客的更新示例.NET Framework检测代码,可以进行更深入的检查
or this 或这个
No registry access . 无注册表访问权限 Borrowed from this blog .
借用这个博客 。
using System;
using System.IO;
using System.Security;
using System.Text.RegularExpressions;
namespace YourNameSpace
{
public class SystemInfo
{
private const string FRAMEWORK_PATH = "\\Microsoft.NET\\Framework";
private const string WINDIR1 = "windir";
private const string WINDIR2 = "SystemRoot";
public static string FrameworkVersion
{
get
{
try
{
return getHighestVersion(NetFrameworkInstallationPath);
}
catch (SecurityException)
{
return "Unknown";
}
}
}
private static string getHighestVersion(string installationPath)
{
string[] versions = Directory.GetDirectories(installationPath, "v*");
string version = "Unknown";
for (int i = versions.Length - 1; i >= 0; i--)
{
version = extractVersion(versions[i]);
if (isNumber(version))
return version;
}
return version;
}
private static string extractVersion(string directory)
{
int startIndex = directory.LastIndexOf("\\") + 2;
return directory.Substring(startIndex, directory.Length - startIndex);
}
private static bool isNumber(string str)
{
return new Regex(@"^[0-9]+\.?[0-9]*$").IsMatch(str);
}
public static string NetFrameworkInstallationPath
{
get { return WindowsPath + FRAMEWORK_PATH; }
}
public static string WindowsPath
{
get
{
string winDir = Environment.GetEnvironmentVariable(WINDIR1);
if (String.IsNullOrEmpty(winDir))
winDir = Environment.GetEnvironmentVariable(WINDIR2);
return winDir;
}
}
}
}
Here is an improved example that includes service packs , 这是一个包含服务包的改进示例 ,
string path = System.Environment.SystemDirectory;
path = path.Substring( 0, path.LastIndexOf('\\') );
path = Path.Combine( path, "Microsoft.NET" );
// C:\WINDOWS\Microsoft.NET\
string[] versions = new string[]{
"Framework\\v1.0.3705",
"Framework64\\v1.0.3705",
"Framework\\v1.1.4322",
"Framework64\\v1.1.4322",
"Framework\\v2.0.50727",
"Framework64\\v2.0.50727",
"Framework\\v3.0",
"Framework64\\v3.0",
"Framework\\v3.5",
"Framework64\\v3.5",
"Framework\\v3.5\\Microsoft .NET Framework 3.5 SP1",
"Framework64\\v3.5\\Microsoft .NET Framework 3.5 SP1",
"Framework\\v4.0",
"Framework64\\v4.0"
};
foreach( string version in versions )
{
string versionPath = Path.Combine( path, version );
DirectoryInfo dir = new DirectoryInfo( versionPath );
if( dir.Exists )
{
Response.Output.Write( "{0}<br/>", version );
}
}
The problem is that you will have to keep up with the versions as they come out. 问题是你必须跟上版本的出现。
You can use the MSI API functions to get a list of all installed products and then check whether the required .NET Framework version is installed. 您可以使用MSI API函数获取所有已安装产品的列表,然后检查是否已安装所需的.NET Framework版本。
Just don't tell you boss that these functions will read from the Registry. 只是不要告诉老板这些函数会从注册表中读取。
Here's the code: 这是代码:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
class Program
{
[DllImport("msi.dll", SetLastError = true)]
static extern int MsiEnumProducts(int iProductIndex, StringBuilder lpProductBuf);
[DllImport("msi.dll", CharSet = CharSet.Unicode)]
static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len);
public const int ERROR_SUCCESS = 0;
public const int ERROR_MORE_DATA = 234;
public const int ERROR_NO_MORE_ITEMS = 259;
static void Main(string[] args)
{
int index = 0;
StringBuilder sb = new StringBuilder(39);
while (MsiEnumProducts(index++, sb) == 0)
{
var productCode = sb.ToString();
var product = new Product(productCode);
Console.WriteLine(product);
}
}
class Product
{
public string ProductCode { get; set; }
public string ProductName { get; set; }
public string ProductVersion { get; set; }
public Product(string productCode)
{
this.ProductCode = productCode;
this.ProductName = GetProperty(productCode, "InstalledProductName");
this.ProductVersion = GetProperty(productCode, "VersionString");
}
public override string ToString()
{
return this.ProductCode + " - Name: " + this.ProductName + ", Version: " + this.ProductVersion;
}
static string GetProperty(string productCode, string name)
{
int size = 0;
int ret = MsiGetProductInfo(productCode, name, null, ref size); if (ret == ERROR_SUCCESS || ret == ERROR_MORE_DATA)
{
StringBuilder buffer = new StringBuilder(++size);
ret = MsiGetProductInfo(productCode, name, buffer, ref size);
if (ret == ERROR_SUCCESS)
return buffer.ToString();
}
throw new System.ComponentModel.Win32Exception(ret);
}
}
}
This page may be of use: http://blogs.msdn.com/b/astebner/archive/2009/06/16/9763379.aspx 此页面可能有用: http : //blogs.msdn.com/b/astebner/archive/2009/06/16/9763379.aspx
Although the registry bit is irrelevant to you, the checking using mscoree.dll may be of help - its just that I cant access skydrive from work hence cant look through the code. 虽然注册表位与您无关,但使用mscoree.dll进行检查可能会有所帮助 - 它只是因为我无法从工作中访问skydrive因此无法查看代码。
Ill see if i find something else. 我看看我是否还能找到别的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.