class Program { static void Main(string[] args) { Console.WriteLine(" OS Version: {0}", Environment.OSVersion); Console.WriteLine(" CLR Version: {0} ( {1} )", Environment.Version, RuntimeFramework.CurrentFramework); Console.WriteLine("Default Encoding: {0}", Encoding.Default); Console.WriteLine(); Console.WriteLine("Available Frameworks:"); foreach (var frame in RuntimeFramework.AvailableFrameworks) Console.WriteLine(" " + frame); Console.ReadLine(); } } public enum RuntimeType { Any, // Any supported runtime framework Net, // Microsoft .NET Framework NetCF, // Microsoft .NET Compact Framework SSCLI, // Microsoft Shared Source CLI Mono, // Mono } // See http://nunit.org, this class from NUnit Project's RuntimeFramework.cs // RuntimeFramework represents a particular version of a common language runtime implementation. [Serializable] public sealed class RuntimeFramework { public RuntimeType Runtime { get; private set; } public Version Version { get; private set; } public string DisplayName { get; private set; } static RuntimeFramework currentFramework; public static RuntimeFramework CurrentFramework { get { if (currentFramework == null) { var monoRuntimeType = Type.GetType("Mono.Runtime", false); var runtime = monoRuntimeType != null ? RuntimeType.Mono : RuntimeType.Net; currentFramework = new RuntimeFramework(runtime, Environment.Version); if (monoRuntimeType != null) { var method = monoRuntimeType.GetMethod("GetDisplayName", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.ExactBinding); if (method != null) currentFramework.DisplayName = (string)method.Invoke(null, new object[0]); } } return currentFramework; } } public static RuntimeFramework[] AvailableFrameworks { get { var frameworks = new List<RuntimeFramework>(); foreach (var framework in GetAvailableFrameworks(RuntimeType.Net)) frameworks.Add(framework); foreach (var framework in GetAvailableFrameworks(RuntimeType.Mono)) frameworks.Add(framework); return frameworks.ToArray(); } } public static bool IsMonoInstalled() { if (CurrentFramework.Runtime == RuntimeType.Mono) return true; // Don't know how to do this on linux yet, but it's not a problem since we are only supporting Mono on Linux if (Environment.OSVersion.Platform != PlatformID.Win32NT) return false; var key = Registry.LocalMachine.OpenSubKey(@"Software\Novell\Mono"); if (key == null) return false; var version = key.GetValue("DefaultCLR") as string; if (string.IsNullOrEmpty(version)) return false; return key.OpenSubKey(version) != null; } // Returns an array of all available frameworks of a given type, for example, all mono or all .NET frameworks. public static RuntimeFramework[] GetAvailableFrameworks(RuntimeType rt) { var frameworks = new List<RuntimeFramework>(); if (rt == RuntimeType.Net && Environment.OSVersion.Platform != PlatformID.Unix) { var key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\.NETFramework\policy"); if (key != null) foreach (var name in key.GetSubKeyNames()) if (name.StartsWith("v")) foreach (var build in key.OpenSubKey(name).GetValueNames()) frameworks.Add(new RuntimeFramework(rt, new Version(name.Substring(1) + "." + build))); } else if (rt == RuntimeType.Mono && IsMonoInstalled()) { var framework = new RuntimeFramework(rt, new Version(1, 1, 4322)); framework.DisplayName = "Mono 1.0 Profile"; frameworks.Add(framework); framework = new RuntimeFramework(rt, new Version(2, 0, 50727)); framework.DisplayName = "Mono 2.0 Profile"; frameworks.Add(framework); } return frameworks.ToArray(); } public RuntimeFramework(RuntimeType runtime, Version version) { Runtime = runtime; Version = version; DisplayName = runtime.ToString() + " " + version.ToString(); } public override string ToString() { return DisplayName; } }这个貌似满足了楼主的要求
http://msdn.microsoft.com/zh-cn/library/vstudio/bb909885.aspx
这个是网页上的。。我要C#或者WPF 后台代码可以检测的。。
怎么没有意义。WPF是.net3.0 就有了。我要检测是否安装了4.0版本的。
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" OS Version: {0}", Environment.OSVersion);
Console.WriteLine(" CLR Version: {0} ( {1} )", Environment.Version, RuntimeFramework.CurrentFramework);
Console.WriteLine("Default Encoding: {0}", Encoding.Default);
Console.WriteLine();
Console.WriteLine("Available Frameworks:");
foreach (var frame in RuntimeFramework.AvailableFrameworks) Console.WriteLine(" " + frame);
Console.ReadLine();
}
} public enum RuntimeType
{
Any, // Any supported runtime framework
Net, // Microsoft .NET Framework
NetCF, // Microsoft .NET Compact Framework
SSCLI, // Microsoft Shared Source CLI
Mono, // Mono
} // See http://nunit.org, this class from NUnit Project's RuntimeFramework.cs
// RuntimeFramework represents a particular version of a common language runtime implementation.
[Serializable]
public sealed class RuntimeFramework
{
public RuntimeType Runtime { get; private set; }
public Version Version { get; private set; }
public string DisplayName { get; private set; }
static RuntimeFramework currentFramework; public static RuntimeFramework CurrentFramework
{
get
{
if (currentFramework == null)
{
var monoRuntimeType = Type.GetType("Mono.Runtime", false);
var runtime = monoRuntimeType != null ? RuntimeType.Mono : RuntimeType.Net;
currentFramework = new RuntimeFramework(runtime, Environment.Version);
if (monoRuntimeType != null)
{
var method = monoRuntimeType.GetMethod("GetDisplayName", BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.ExactBinding);
if (method != null) currentFramework.DisplayName = (string)method.Invoke(null, new object[0]);
}
}
return currentFramework;
}
} public static RuntimeFramework[] AvailableFrameworks
{
get
{
var frameworks = new List<RuntimeFramework>();
foreach (var framework in GetAvailableFrameworks(RuntimeType.Net)) frameworks.Add(framework);
foreach (var framework in GetAvailableFrameworks(RuntimeType.Mono)) frameworks.Add(framework);
return frameworks.ToArray();
}
} public static bool IsMonoInstalled()
{
if (CurrentFramework.Runtime == RuntimeType.Mono) return true;
// Don't know how to do this on linux yet, but it's not a problem since we are only supporting Mono on Linux
if (Environment.OSVersion.Platform != PlatformID.Win32NT) return false;
var key = Registry.LocalMachine.OpenSubKey(@"Software\Novell\Mono");
if (key == null) return false;
var version = key.GetValue("DefaultCLR") as string;
if (string.IsNullOrEmpty(version)) return false;
return key.OpenSubKey(version) != null;
} // Returns an array of all available frameworks of a given type, for example, all mono or all .NET frameworks.
public static RuntimeFramework[] GetAvailableFrameworks(RuntimeType rt)
{
var frameworks = new List<RuntimeFramework>();
if (rt == RuntimeType.Net && Environment.OSVersion.Platform != PlatformID.Unix)
{
var key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\.NETFramework\policy");
if (key != null)
foreach (var name in key.GetSubKeyNames())
if (name.StartsWith("v"))
foreach (var build in key.OpenSubKey(name).GetValueNames())
frameworks.Add(new RuntimeFramework(rt, new Version(name.Substring(1) + "." + build)));
}
else if (rt == RuntimeType.Mono && IsMonoInstalled())
{
var framework = new RuntimeFramework(rt, new Version(1, 1, 4322));
framework.DisplayName = "Mono 1.0 Profile";
frameworks.Add(framework);
framework = new RuntimeFramework(rt, new Version(2, 0, 50727));
framework.DisplayName = "Mono 2.0 Profile";
frameworks.Add(framework);
}
return frameworks.ToArray();
} public RuntimeFramework(RuntimeType runtime, Version version)
{
Runtime = runtime;
Version = version;
DisplayName = runtime.ToString() + " " + version.ToString();
} public override string ToString()
{
return DisplayName;
}
}这个貌似满足了楼主的要求
RegistryKey rk;
rk = Registry.LocalMachine.OpenSubKey(路径);
后面的自己看属性吧
win7 例:C:\Windows\Microsoft.NET\Framework 下查看是否有4.0另外一个:http://blog.csdn.net/yysyangyangyangshan/article/details/7039769
如果机器上就没有.NET FRAMEWORK,程序都运行不起来怎么判断呀
我一般是用VB写一个检测程序,如果有.NET 运行环境,就运行.NET的程序,没有就给出相关提示