代码改变世界

获取当前操作系统的版本

2009-01-20 14:46  yufun  阅读(610)  评论(0编辑  收藏  举报

OperatingSystem os = Environment.OSVersion;
switch (os.Platform)
{
    case PlatformID.Win32NT:
        switch (os.Version.Major)
        {
            case 3:
                return "Windows NT 3.51";
            case 4:
                return "Windows NT 4.0";
            case 5:
                switch (os.Version.Minor)
                {
                    case 0:
                        return "Windows 2000";
                    case 1:
                        return "Windows XP";
                    case 2:
                        return "Windows 2003";
                    default:
                        return "";
                }
            case 6:
                return "Windows Vista";
            default:
                return "";
        }
    case PlatformID.Win32S:
        return "";
    case PlatformID.Win32Windows:
        switch (os.Version.Minor)
        {
            case 0:
                return "Windows 95";
            case 10:
                if (os.Version.Revision.ToString() == "2222A")
                {
                    return "Windows 98 SE";
                }
                else
                {
                    return "Windows 98";
                }
            case 90:
                return "Windows Me";
            default:
                return "";
        }
    default:
        return "";
}

 

第二种比较简单的:

string rootKey = "HKEY_LOCAL_MACHINE";
string subKey = rootKey + @"\SOFTWARE\Microsoft\Windows NT\CurrentVersion";
object value = Registry.GetValue(subKey, "ProductName", null);
if (value != null)
{
    return value as string;
}
else
{
    return "";
}