首页 > 其他 > 详细

如何获取当前操作系统,iis版本号及framework版本

时间:2014-10-30 22:48:44      阅读:464      评论:0      收藏:0      [点我收藏+]

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SystemInfo info = new SystemInfo();
            Console.WriteLine(info.PCName);
            Console.WriteLine(info.IIS);
            //Console.WriteLine(info.OSVersion);
            Console.WriteLine(info.Framework);
            Console.ReadLine();
        }
    }

    public class SystemInfo
    {
        //操作系统名称
        private string pcName;
        public string PCName
        {
            get
            {
                //如果操作系统为空则获取一下
                if (string.IsNullOrEmpty(this.pcName))
                {
                    RegistryKey rk;
                    rk = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion");
                    this.pcName = rk.GetValue("ProductName").ToString();
                }
                return "当前操作系统:"+this.pcName;
            }
        }

        private int osVersion;
        public int OSVersion
        {
            get
            {
                if (osVersion == 0)
                {
                    RegistryKey rk;
                    rk = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion");
                    int.TryParse(rk.GetValue("CurrentBuildNumber").ToString(), out this.osVersion);
                }

                return this.osVersion;
            }
        }

        //IIS版本号
        private string iis;
        public string IIS
        {
            get
            {
                //如果IIS为空则获取
                if (string.IsNullOrEmpty(this.iis))
                {
                    RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\InetStp");
                    this.iis = "IIS" + key.GetValue("MajorVersion").ToString();
                }
                return "IIS版本:"+this.iis;
            }
        }

        //framework版本号
        private string framework;
        public string Framework
        {
            get
            {
                //如果framework版本号为空则获取
                if (string.IsNullOrEmpty(this.framework))
                {
                    Version v = Environment.Version;
                    if (v != null)
                    {
                        this.framework = v.Major + "." + v.Minor;
                    }
                }
                return "Framework版本:Framework" + this.framework;
            }
        }
    }
}

如何获取当前操作系统,iis版本号及framework版本

原文:http://my.oschina.net/u/855028/blog/339033

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!