views:

586

answers:

9

How can I detect Windows 7 (versions) in .net?

What code can I use?

+4  A: 

How to determine the Windows version by using Visual C# at http://support.microsoft.com/kb/304283

Dave Swersky
Article doesn't cover Vista, Windows 7, Windows Server 2003 and 2008.
AMissico
+3  A: 

via Environment.OSVersion which "Gets an System.OperatingSystem object that contains the current platform identifier and version number."

Gregoire
-1 for not answering the question.
AMissico
+1 for answering the question, the user could google for the correct ID, Amissico are you on a rampage?
PoweRoy
@PoweRoy: One of my pet peeves is not answering the question. Yet, you are absolutely correct. This was a simple enough question that hosseinsinohe should have search and not asked. I missed that. Thank you for pointing it out. I will try and fix.
AMissico
Fixed, but I had to edit the anwers. I don't understand why I can't unvote whenever I want. It is my vote for Pete's Sake.
AMissico
@PoweRoy: Again, thanks for pointing this out to me. I really do appreciate it.
AMissico
+29  A: 

System.Environment.OSVersion has the information you need. It consists of three components which map to the following Windows versions:

+----------------------------------------------------------------------------------------------+
|           |Windows|Windows|Windows|Windows NT|Windows|Windows|Windows|Windows|Windows|Windows|
|           |  95   |  98   |  Me   |    4.0   | 2000  |  XP   |  2003 | Vista |  2008 |  7    |
+----------------------------------------------------------------------------------------------+
|PlatformID | 1     | 1     | 1     | 2        | 2     | 2     |   2   |   2   |   2   |   2   |
+----------------------------------------------------------------------------------------------+
|Major      |       |       |       |          |       |       |       |       |       |       |
| version   | 4     | 4     | 4     | 4        | 5     | 5     |   5   |   6   |   6   |   6   |
+----------------------------------------------------------------------------------------------+
|Minor      |       |       |       |          |       |       |       |       |       |       |
| version   | 0     | 10    | 90    | 0        | 0     | 1     |   2   |   0   |   1   |   2   |
+----------------------------------------------------------------------------------------------+

EDIT: updated with more versions from this link

Daniel DiPaolo
The question was about Windows 7, your graph only goes to XP... :)
Nate Bross
While System.Environment.OSVersion can be used, your answer desn't actually explain how to detect Windows 7!
Daniel Renshaw
Run it on Windows 7 and find out?
user144182
-1 for not answering the question.
AMissico
You guys caught me between edits, the table from the link doesn't tell you but I found a link that does and updated the table.
Daniel DiPaolo
:O) Then you shouldn't have posted until you answered the question. Yet, I do the same thing. I lost so many posts because there is no saving. So, I post multiple times. I wish there was a standalone application where we could edit in "markdown", save, continue editing offline then post when finish.
AMissico
This really doesn't answer the question, there's no example of code but rather a chart of version numbers.
gmcalab
How does referencing the property that contains these version numbers and stating what the appropriate combinations map to *not* answer the question? You don't always have to give code to answer a problem.
Daniel DiPaolo
The question asked for a coding example, so its obvious they don't know how to implement it. That's how.
gmcalab
Perhaps Daniel enjoys teaching folks to fish.
sixlettervariables
+18  A: 

I used this when I had to determine various Microsoft Operating System versions:

string getOSInfo()
{
   //Get Operating system information.
   OperatingSystem os = Environment.OSVersion;
   //Get version information about the os.
   Version vs = os.Version;

   //Variable to hold our return value
   string operatingSystem = "";

   if (os.Platform == PlatformID.Win32Windows)
   {
       //This is a pre-NT version of Windows
       switch (vs.Minor)
       {
           case 0:
               operatingSystem = "95";
               break;
           case 10:
               if (vs.Revision.ToString() == "2222A")
                   operatingSystem = "98SE";
               else
                   operatingSystem = "98";
               break;
           case 90:
               operatingSystem = "Me";
               break;
           default:
               break;
       }
   }
   else if (os.Platform == PlatformID.Win32NT)
   {
       switch (vs.Major)
       {
           case 3:
               operatingSystem = "NT 3.51";
               break;
           case 4:
               operatingSystem = "NT 4.0";
               break;
           case 5:
               if (vs.Minor == 0)
                   operatingSystem = "2000";
               else
                   operatingSystem = "XP";
               break;
           case 6:
               if (vs.Minor == 0)
                   operatingSystem = "Vista";
               else
                   operatingSystem = "7";
               break;
           default:
               break;
       }
   }
   //Make sure we actually got something in our OS check
   //We don't want to just return " Service Pack 2" or " 32-bit"
   //That information is useless without the OS version.
   if (operatingSystem != "")
   {
       //Got something.  Let's prepend "Windows" and get more info.
       operatingSystem = "Windows " + operatingSystem;
       //See if there's a service pack installed.
       if (os.ServicePack != "")
       {
           //Append it to the OS name.  i.e. "Windows XP Service Pack 3"
           operatingSystem += " " + os.ServicePack;
       }
       //Append the OS architecture.  i.e. "Windows XP Service Pack 3 32-bit"
       //operatingSystem += " " + getOSArchitecture().ToString() + "-bit";
   }
   //Return the information we've gathered.
   return operatingSystem;
}

Source: here

gmcalab
Perhaps the `m$` reference?
sixlettervariables
Oh, my bad I thought someone changed it. Fixed.
gmcalab
What about the getOSArchitecture() method?Error: "The name 'getOSArchitecture' does not exist in the current context."
LonnieBest
@LonnieBest - That is a method that determines whether it's x64 or x86... I didn't use that, just comment it out.
gmcalab
+5  A: 

Like always - you use System.Environment.OSVersion (see TechNet - How to determine the Windows version by using Visual C#):

var osInfo = System.Environment.OSVersion;
// see osInfo.Version 
tanascius
-1 for not answering the question.
AMissico
A: 

http://msdn.microsoft.com/en-us/library/ms724358.aspx Please check this information

Sundararajan S
This is Not Related!!!
hosseinsinohe
Did we not notice this? C# - "This language is not supported, or no code example is available."
Jace Rhea
+2  A: 

Whenever someone asks "How?" I always tend to think of "Why?"

If you're detecting Windows 7 to use Windows 7 specific features, consider using the Windows® API Code Pack for Microsoft® .NET Framework library instead.

R. Bemrose
Helpful, but probably best left as a comment since it doesn't answer the question.
Jace Rhea
@jacerhea: I could have (and in fact, did, which I deleted when I changed it to an answer), but comments tend to get lost in the shuffle. Also, the comment editor sucks, as you can't hyperlink text in it.
R. Bemrose
A: 

one way:

public string GetOSVersion()
{
    int _MajorVersion = Environment.OSVersion.Version.Major;

    switch (_MajorVersion) {
        case 5:
        return "Windows XP";
    case 6:
        switch (Environment.OSVersion.Version.Minor) {
            case 0:
                return "Windows Vista";
            case 1:
                return "Windows 7";
            default:
                return "Windows Vista & above";
        }
        break;
    default:
        return "Unknown";
}
}

Then simply do wrap a select case around the function..

Darknight
Not relevant to the original post, but if the major version is 5 then you have three possible versions of Windows: 5.0 = Windows 2000, 5.1 = 32-bit Windows XP, 5.2 = Windows Server 2003 or 64-bit Windows XP
BACON
+3  A: 

Like R. Bemrose suggested, if you are doing Windows 7 specific features, you should look at the Windows® API Code Pack for Microsoft® .NET Framework.

It contains a CoreHelpers class that let you determine the OS you are currently on (XP and above only, its a requirement for .NET nowaday)

It also provide multiple helper methods. For example, suppose that you want to use the jump list of Windows 7, there is a class TaskbarManager that provide a property called IsPlatformSupported and it will return true if you are on Windows 7 and above.

Pierre-Alain Vigeant