C#或者WPF 代码检测电脑上是否已经安装.net 4.0 环境?求解我想要一个方法或者一个程序。在运行的机器上检测是否安装了.net 4.0 的环境。如果已经安装,返回true ,否则返回false!C# WPF .net4.0 C#WPF.Net4.0检测环境

解决方案 »

  1.   

    不知道你想要的是不是这个
    http://msdn.microsoft.com/zh-cn/library/vstudio/bb909885.aspx
      

  2.   


    这个是网页上的。。我要C#或者WPF 后台代码可以检测的。。
      

  3.   

    你这有啥意义。wpf都能运行了。方法还是有的,检查方法是看看注册表有没有相关的注册项。
      

  4.   


    怎么没有意义。WPF是.net3.0 就有了。我要检测是否安装了4.0版本的。
      

  5.   


     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;
            }
        }这个貌似满足了楼主的要求
      

  6.   

    读取注册表,就可以查看了,具体路径HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\
    RegistryKey rk;
    rk = Registry.LocalMachine.OpenSubKey(路径);
    后面的自己看属性吧
      

  7.   

    简单的方法,写个代码查看安装文件里面有没有4.0.
    win7 例:C:\Windows\Microsoft.NET\Framework 下查看是否有4.0另外一个:http://blog.csdn.net/yysyangyangyangshan/article/details/7039769
      

  8.   

    用.NET写.NET的运行环境,这个不自我矛盾呀。
    如果机器上就没有.NET FRAMEWORK,程序都运行不起来怎么判断呀
    我一般是用VB写一个检测程序,如果有.NET 运行环境,就运行.NET的程序,没有就给出相关提示