Recipe20.10.Determining the Operating System and Service Pack Version of the Current Operating System


Recipe 20.10. Determining the Operating System and Service Pack Version of the Current Operating System

Problem

You want to know the current operating system and service pack.

Solution

Use the GetOSAndServicePack method shown in Example 20-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, 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 20-2. GetOSAndServicePack method

 public static string GetOSAndServicePack( ) {     // Get the current OS info.     OperatingSystem os = Environment.OSVersion;     string osText = "";     // If version is 5, then it is Win2K, XP, or 2003     if (os.Version.Major == 5)     {         switch (os.Version.Minor)         {         case 0: osText = "Windows 2000";             break;         case 1: osText = "Windows XP";             break;         case 2: osText = "Windows Server 2003";             break;         // This is the default but it usually reports "Microsoft Windows NT"         // due to relying on the PlatformID.         default: osText = os.ToString( );             break;         }     }     else     {         // Probably NT4 as .NET doesn't run on Win9X…         osText = os.VersionString;     }     // Get the text for the service pack.     string spText = os.ServicePack;     // Build the whole string.     return string.Format("{0} {1}", osText, spText); } 

Discussion

Enabling your application to know the current operating system and service pack allows you to include that information in debugging reports and in the about box (if you have one) for your application. The simple knowledge of the correct operating system and service pack transmitted through your support department can save you hours in debugging time. It is well worth making available so your support department can easily direct your clients to it in case they cannot otherwise locate it.

See Also

See the "Environment.OSVersion Property" and "OperatingSystem Class" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net