没有Registry确定框架版本的方法

时间:2011-08-01 14:13:11

标签: c# .net frameworks

我已经搜索了很长时间,但我找不到答案。 有没有办法确定PC上安装的框架和Service Pack .NET,而无需访问C#中的注册表? 我可以使用注册表项,但我必须这样做而无需访问注册表。 我在C:\ Windows \ Microsoft .NET中读到了一些关于目录的内容,但在那里,我只发现了框架版本,没有关于SP的内容。但我需要框架和Service Pack。 有人可以帮帮我吗?

问候,格雷格

6 个答案:

答案 0 :(得分:4)

string clrVersion = System.Environment.Version.ToString();
string dotNetVersion = Assembly
                      .GetExecutingAssembly()
                      .GetReferencedAssemblies()
                      .Where(x => x.Name == "mscorlib").First().Version.ToString();

答案 1 :(得分:2)

您可以使用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"]);
            }


        }
    }

答案 2 :(得分:0)

我认为可以问WMI。

查询所有Win32_Product元素并查找Name属性包含“Microsoft .NET Framework”

WMI也提供了ServicePack信息..但我不确切知道在哪里。

答案 3 :(得分:0)

你可以这样做:

System.Environment.Version.ToString()

仅限CLR版本

或此

来自MSDN博客Updated sample .NET Framework detection code that does more in-depth checking

或此

无注册表访问 。借用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 );
    }
}

问题是你必须跟上版本的出现。

答案 4 :(得分:0)

您可以使用MSI API函数获取所有已安装产品的列表,然后检查是否已安装所需的.NET Framework版本。

请不要告诉老板,这些功能会从注册表中读取。

以下是代码:

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);
        }
    }
}

答案 5 :(得分:-1)

此页面可能有用: http://blogs.msdn.com/b/astebner/archive/2009/06/16/9763379.aspx

虽然注册表位与您无关,但使用mscoree.dll进行检查可能会有所帮助 - 它只是因为我无法从工作中访问skydrive因此无法查看代码。

我看看我是否还能找到别的东西。