首页 > 代码库 > 如何获取当前操作系统,iis版本号及framework版本

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

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