如何在C#应用程序中获取.NET框架目录路径?
我所指的文件夹是"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727“
发布于 2008-12-17 20:39:34
一种更简单的方法是包含Microsoft.Build.Utilities程序集并使用
using Microsoft.Build.Utilities;
ToolLocationHelper.GetPathToDotNetFramework(
TargetDotNetFrameworkVersion.VersionLatest);
发布于 2008-12-17 20:18:04
您可以从Windows注册表中获取它:
using System;
using Microsoft.Win32;
// ...
public static string GetFrameworkDirectory()
{
// This is the location of the .Net Framework Registry Key
string framworkRegPath = @"Software\Microsoft\.NetFramework";
// Get a non-writable key from the registry
RegistryKey netFramework = Registry.LocalMachine.OpenSubKey(framworkRegPath, false);
// Retrieve the install root path for the framework
string installRoot = netFramework.GetValue("InstallRoot").ToString();
// Retrieve the version of the framework executing this program
string version = string.Format(@"v{0}.{1}.{2}\",
Environment.Version.Major,
Environment.Version.Minor,
Environment.Version.Build);
// Return the path of the framework
return System.IO.Path.Combine(installRoot, version);
}
发布于 2020-11-26 23:38:03
对于.NET框架版本>= 4.5、an official way from MSDN
internal static class DotNetFrameworkLocator
{
public static string GetInstallationLocation()
{
const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
{
if (ndpKey == null)
throw new Exception();
var value = ndpKey.GetValue("InstallPath") as string;
if (value != null)
return value;
else
throw new Exception();
}
}
}
https://stackoverflow.com/questions/375860
复制相似问题