CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2009
    Posts
    73

    Question How to programmatically conclude whether the OS is Windows XP

    Hi,

    I'm looking for a C# code snippet which concludes whether the OS is Windows XP, which is bullet proof and not risky (i.e., no strings manipulation on the name of the OS, since maybe there are Windows XP Editions which don't contain the exact word "Windows XP", and I wouldn't go for "string.Contains("XP")" either) ?

    [If you have the same code snippet for Vista and Windows 7, it will be great]

    Thanks at Advanced!

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to programmatically conclude whether the OS is Windows XP

    Envirnment.OSVersion

    While I also like to avoid string manipulation when possible, I don't think worrying about Windows XP versions that don't contain the string "XP" is a valid concern as they all do and information on every version of windows out there is readily available. You could store each version string as a const field and then just use a switch to determine the version. That can return an enumerated value if you like.

    You don't need to be overly defensive here.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to programmatically conclude whether the OS is Windows XP

    Use the Environment.OSVersion.Version to get the major and minor versions.

    Code:
    OS                                      Version number 
    Windows 7                          6.1 
    Windows Server 2008 R2   6.1 
    Windows Server 2008        6.0 
    Windows Vista                    6.0 
    Windows Server 2003 R2   5.2 
    Windows Server 2003        5.2 
    Windows XP 64-Bit Edition 5.2 
    Windows XP                       5.1 
    Windows 2000                   5.0

    You may notice that this chart doesn't distinguish between the server and desktop versions, nor does it give you info on specific editions.

    If you need this additional info, then you'll need to pinvoke to GetVersionEx.

    See OSVERSIONINFOEX for more info.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to programmatically conclude whether the OS is Windows XP

    Good call Arjay; for some reason I thought OSVersion was a string.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured