Getting Windows Vista and Windows 7 OS name using C#

With Windows Vista getting popular and Windows 7 fast approaching there will soon be a lot of applications running on these platform. Some of the scenarios need to read the OS name dynamically from code. The commonly used method is to extract the OS name using System.OperatingSystem object. It provides a Platform and version number using which you can deduce the OS name.

I have done the same here and added the Version checks for Windows Vista and Windows 7. Windows Vista and Windows 7 come under the Win32NT platform.

Windows Vista has Major version number 6 and minor version number as 0.

Windows 7 has Major version number 6 and minor version number as 1.

And here is the routine,

public string GetOpertatingSystemName()
{
    System.OperatingSystem os = System.Environment.OSVersion;
    string name = "Unknown";

    switch (os.Platform)
    {
        case System.PlatformID.Win32Windows:
            switch (os.Version.Minor)
            {
                case 0:
                    name = "Windows 95";
                    break;

                case 10:
                    name = "Windows 98";
                    break;

                case 90:
                    name = "Windows ME";
                    break;
            }
            break;

        case System.PlatformID.Win32NT:
            switch (os.Version.Major)
            {
                case 3:
                    name = "Windws NT 3.51";
                    break;

                case 4:
                    name = "Windows NT 4";
                    break;

                case 5:
                    switch (os.Version.Minor)
                    {
                        case 0:
                            name = "Windows 2000";
                            break;
                        case 1:
                            name = "Windows XP";
                            break;
                        case 2:
                            name = "Windows Server 2003";
                            break;
                    }
                    break;

                case 6:
                    switch (os.Version.Minor)
                    {
                        case 0:
                            name = "Windows Vista";
                            break;
                        case 1:
                            name = "Windows 7";
                            break;
                    }
                    break;
            }
            break;
    }

    return name;
}

Thanks to this article on CodeGuru for the version information.

Have fun.

Tags: ,

4 Responses to “Getting Windows Vista and Windows 7 OS name using C#”

  1. Scott Says:

    Thanks. I owe you this one.

  2. f Says:

    sdf

  3. Peter Says:

    hey Julius,

    Thank you.

  4. kallyhoophy Says:

    Other variant is possible also

Leave a Reply