Feb 03
This article explains how to use C# to determine the name, edition, service pack, version and bits of the host operating system.
For example, the results on my PC would be:
Operation System Information
—————————-
Name = Windows Vista
Edition = Home Premium
Service Pack = Service Pack 1
Version = 6.0.6001.65536
Bits = 64
Code
You can download the code here.
Sample Program
Here is a simple console program that demonstrates this:
using System; namespace CSharp411 { class Program { static void Main( string[] args ) { Console.WriteLine( "Operation System Information" ); Console.WriteLine( "----------------------------" ); Console.WriteLine( "Name = {0}", OSInfo.Name ); Console.WriteLine( "Edition = {0}", OSInfo.Edition ); Console.WriteLine( "Service Pack = {0}", OSInfo.ServicePack ); Console.WriteLine( "Version = {0}", OSInfo.VersionString ); Console.WriteLine( "Bits = {0}", OSInfo.Bits ); Console.ReadLine(); } } }
References
The code for this article was derived from some excellent articles on the subject:
Awesome! Plugged it right in and it worked. Thanks for providing it.
wonderful!thanks this code is very helpful.
Hi, thanks for this – Works a treat for Operating System’s up to Vista. For it to detect Windows 7, I amended the Windows Vista Detection to look like this:
case 6:
switch (minorVersion)
{
case 0:
name = “Windows Vista”;
break;
case 1:
name = “Windows 7”;
break;
case 3:
name = “Windows Server 2008”;
break;
}
break;
Seems to work a treat, but haven’t tested on Windows Vista. I now want to know how I would go about fixing the Product part, so it will show ‘Professional’ etc. (I think Home Premium, Basic and Ultimate should still be working).
Any help is appreciated 😀
Have you a way of discriminating between XP Professional and XP Tablet?
Ok I have found it!!
To discriminate if you have a Tablet PC running XP
Inside #region EDITION I added
[DLLImport(“user32”)]
public static extern int GetSystemMetrics(int nIndex);
and
in #region 5 made this change
if ((suitMask & VER_SUITE_PERSONAL) != 0)
{
edition = “Home”;
}
else
{
if (GetSystemMetrics(86) == 0) // 86 == SM_TABLETPC
edition = “Professional”;
else
edition = “Tablet Edition”;
}
I was able to test only on an XP Tablet and an XP Professional laptops.
Thanks for a terrific article and Jonney Tiney fb.
Awsome!
Ok, I’m devloping using W7 64-bit, but my app needs to run on both 32 and 64 bit. No problem so far, except I’m using SQLite3. There are two different DLLs depending on the OS version. So, how can I use the detection code to dynamically select which version of the DLL to load?
+ I opened VS2010.
+ I Created new C# Console Project in .NF 2.0
+ I Copied the code above and pasted it into the Code after deleting the premade VS code.
+ Set it to Release mode.
+ Compiled and got this error:
——————————————————
Error 1 Program ‘c:usersownerdocumentsvisual studio 2010ProjectsOSCheck-ConsoleOSCheck-Consoleobjx86ReleaseOSCheck-Console.exe’ does not contain a static ‘Main’ method suitable for an entry point OSCheck-Console
——————————————————
What did I do wrong?
@SP1K: For some reason your program is lacking a Main method:
[STAThread]
static void Main()
{
// start your program here
}
Can you send me an email at the email I posted. Thank you.
To cope with XP 64-bit I made this change in
#region 2000, XP, 2003
case 5:
switch (minorVersion)
case 2:
if (productType == VER_NT_WORKSTATION)
name = “Windows XP”; // handles XP 64-bit
else
name = “Windows Server 2003”;
break;
this doesn’t work. it tells u the platform (64/32 bit) of code that runs the context. not the undelying os.
It looks a little weird I do agree – but if I do not trap it like this the code returns Windows 2003 Server 64-bit. My XP SP2 64-bit reports the build as 5.2.3790.131072
full c# code example:
http://mdb-blog.blogspot.com/2010/11/how-to-determine-windows-version-by.html
This is great! Thank you very much!
The Professional edition of Windows 7 was not detected by the original code.
So I changed the following code:
In the PRODUCT region I added the constant:
private const int PRODUCT_PROFESSIONAL = 0x00000030;
In the Edition pproperty and “VERSION 6” region, I added the case:
case PRODUCT_PROFESSIONAL:
edition = “Professional”;
break;
I don’t know whether it works on other “Professional” editions, I just debugged on my Windows 7 Professional 64-bit, and I found the value added in the code.
I’m coding a C# Program and need to use System.Management namespace. but when i type ‘using System.M’ intellisence doesn’t show Management namespace and compiler also not locates a class of that namespace (which I actually need to use— EventArrivedEventArgs). I’m using VS 2005 Plz Help me.!!!
Thanks!
Georgiana,
You may need to add the reference from the solution window and make sure you have the correct dotnet version. I dont think management will work in dotnet 2.0.
Thanks, but OSInfo is not resolved by Visual Studio. (Does not exist in the current context). Apparently, just using the System Namespace isn’t enough because OSInfo is not understood. so this doesn’t work in .NET 4. Microsoft changes things constantly and does a poor job of documentation.