December 2007
Intermediate to advanced
896 pages
19h 57m
English
You want to know the current operating system and service pack.
Use the GetOSAndServicePack method shown in Example 19-2 to get a string representing the current operating system and service pack. GetOSAndServicePack uses the Environment.OSVersion property to get the version information for the operating system and then determines the "official" name of the OS from that. The OperatingSystem class retrieved from Environment.OSVersion has a property for the service pack called ServicePack. The two strings are then merged together and returned as the OS and service pack string.
Example 19-2. GetOSAndServicePack method
public static string GetOSAndServicePack() { // Get the current OS info OperatingSystem os = Environment.OSVersion; string osText = string.Empty; // if version is 5, then it is Win2K, XP, or 2003 switch (os.Version.Major) { case 5: switch (os.Version.Minor) { case 0: osText = "Windows 2000"; break; case 1: osText = "Windows XP"; break; case 2: osText = "Windows Server 2003"; break; default: osText = os.ToString(); break; } break; case 6: switch (os.Version.Minor) { case 0: osText = "Windows Vista"; break; case 1: osText = "Windows Server 2008"; break; default: osText = os.ToString(); break; } break; } if (!string.IsNullOrEmpty(osText)) { // get the text for the service pack string spText = os.ServicePack; // build the whole string return string.Format("{0} {1}", ...Read now
Unlock full access