得到操作系统名称 #region 得到操作系统名称 string GetOperatingSystemName() ...{ string OSName = null; System.OperatingSystem osInfo = System.Environment.OSVersion; switch (osInfo.Platform) ...{ case PlatformID.Unix: break; case PlatformID.Win32NT: switch (osInfo.Version.Major) ...{ case 3: OSName = "Windows NT 3.51"; break; case 4: OSName = "Windows NT 4.0"; break; case 5: switch (osInfo.Version.Minor) ...{ case 0: OSName = "Windows 2000"; break; case 1: OSName = "Windows XP"; break; case 2: OSName = "Windows 2003"; break; default: break; } break; case 6: OSName = "Windows Vista"; break; default: OSName = "Unknown Win32NT Windows"; break; } break; case PlatformID.Win32S: break; case PlatformID.Win32Windows: switch (osInfo.Version.Major) ...{ case 0: OSName = "Windows 95"; break; case 10: if (osInfo.Version.Revision.ToString() == "2222A") OSName = "Windows 98 Second Edition"; else OSName = "Windows 98"; break; case 90: OSName = "Windows ME"; break; default: OSName = "Unknown Win32 Windows"; break; } break; case PlatformID.WinCE: break; default: break; } if (osInfo.ServicePack != null) OSName += " " + osInfo.ServicePack; return OSName + string.Format(" ({0})", osInfo.VersionString); } #endregion 另一种方法是使用WMI,代码如下: 得到操作系统信息 #region 得到操作系统信息 DataTable GetOperatingSystem() ...{ DataTable dt = new DataTable(); dt.Columns.Add("EnglishKey", typeof(string)); dt.Columns.Add("ChineseKey", typeof(string)); dt.Columns.Add("Value", typeof(string)); ManagementClass osClass = new ManagementClass("Win32_OperatingSystem"); foreach (ManagementObject obj in osClass.GetInstances()) ...{ DataRow drNew = null; if (obj["Caption"] != null) ...{ drNew = dt.NewRow(); drNew["EnglishKey"] = "Operating System"; drNew["ChineseKey"] = "操作系统"; drNew["Value"] = obj["Caption"].ToString(); if (obj["OtherTypeDescription"] != null) drNew["Value"] = string.Format("{0} {1}", drNew["Value"].ToString(), obj["OtherTypeDescription"].ToString()); if (obj["CSDVersion"] != null) drNew["Value"] = string.Format("{0} {1}", drNew["Value"].ToString(), obj["CSDVersion"].ToString()); if (obj["Version"] != null) drNew["Value"] = string.Format("{0} ({1})", drNew["Value"].ToString(), obj["Version"].ToString()); dt.Rows.Add(drNew); } if (obj["WindowsDirectory"] != null) ...{ drNew = dt.NewRow(); drNew["EnglishKey"] = "Windows Directory"; drNew["ChineseKey"] = "Windows目录"; drNew["Value"] = obj["WindowsDirectory"].ToString(); dt.Rows.Add(drNew); } if (obj["SystemDirectory"] != null) ...{ drNew = dt.NewRow(); drNew["EnglishKey"] = "System Directory"; drNew["ChineseKey"] = "系统目录"; drNew["Value"] = obj["SystemDirectory"].ToString(); dt.Rows.Add(drNew); } if (obj["LastBootUpTime"] != null) ...{ drNew = dt.NewRow(); drNew["EnglishKey"] = "Last Boot Up Time"; drNew["ChineseKey"] = "上次启动日期"; drNew["Value"] = FormatNativeDateTimeString(obj["LastBootUpTime"].ToString()); dt.Rows.Add(drNew); } if (obj["TotalVisibleMemorySize"] != null) ...{ drNew = dt.NewRow(); drNew["EnglishKey"] = "Total Visible Memory Size"; drNew["ChineseKey"] = "Windows可见内存"; drNew["Value"] = string.Format("{0:###,###,###}KB", (ulong)obj["TotalVisibleMemorySize"]); dt.Rows.Add(drNew); } } return dt; } string FormatNativeDateTimeString(string DateTimeString) ...{ return string.Format("{0}-{1}-{2} {3}:{4}:{5}", DateTimeString.Substring(0, 4), DateTimeString.Substring(4, 2), DateTimeString.Substring(6, 2), DateTimeString.Substring(8, 2), DateTimeString.Substring(10, 2), DateTimeString.Substring(12, 2)); } #endregion