码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-10-30 22:50:13      阅读:445      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   sp   on   bs   ad   line   new   

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版本

标签:io   os   ar   sp   on   bs   ad   line   new   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!