没有注册表确定框架版本的方法

本文关键字:版本 方法 框架 注册表 | 更新日期: 2023-09-27 18:04:22

我找了很久,就是找不到答案。有没有办法确定框架和服务包。net安装在PC上,没有访问注册表在c# ?我可以使用注册表项,但我必须这样做没有访问注册表。我读了一些关于C:'Windows'Microsoft . net目录的信息,但是在那里,我只找到了框架版本,没有关于SP的。但是我需要框架和Service Pack。有人能帮帮我吗?

问候,格雷格

没有注册表确定框架版本的方法

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

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

        }
    }

我想可以问问WMI。

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

ServicePack信息也由WMI提供。但我不知道具体在哪里

你可以这样做:

System.Environment.Version.ToString()

(CLR Version only)

来自MSDN博客更新样例。net框架检测代码,做更深入的检查

没有注册表访问。从这个博客借用。

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

这里是一个改进的例子,包括服务包

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

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

只是不要告诉你的老板这些函数将从注册表中读取。

代码如下:

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

本页可能有以下用途:http://blogs.msdn.com/b/astebner/archive/2009/06/16/9763379.aspx

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

我看看能不能找到别的