tags:

views:

42

answers:

2

The following piece of code

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            OperatingSystem os = Environment.OSVersion;
            Console.WriteLine( os.Version.Major.ToString());
            Console.ReadLine();
        }
    }
}

Outputs 6 on both Vista and Win7 How can I tell if my code is running on Win7 or pre Win7 This is using .NET frameworks 2.0

+1  A: 

Look at the Version.Minor property. It is 0 for Vista, 1 for Windows 7.

In other words, your code could be:

        OperatingSystem os = Environment.OSVersion;
        string version;
        if ( os.Version.Major < 6 ) 
            version = "Older Windows";
        else if (os.Version.Major == 6 ) 
        {
             if (os.Version.Minor == 0 ) 
                 version = "Vista";
             if (os.Version.Minor == 1 ) 
                 version = "Windows 7"
        }
driis
+3  A: 

Check the Minor version. 6.0 is Vista, 6.1 is Windows7.

Check this excellent post about detecting OS version, and read the discussion for differentiating servers from workstations as well.

Mikael Svenson
Thanks for linking to my blog. I'm glad it's helpful. There's also a "Part 2" to that post with slightly more complex code which is able to tell the difference between client and server OSes, and even the different versions within Windows 7, Vista, and XP (Home, Professional, etc.). Link: http://andrewensley.com/2009/10/c-detect-windows-os-version-%E2%80%93-part-2-wmi/
Andrew