3

I have a C# application which is expected to run in both WIn 7 & Win XP. I need to check the OS NAME in my C# source code before distributing the MSI & EXE to customers.

Without getting into finer versioning details my code wants to check if it is a 32 bit WINDOWS XP or a 64-bit WINDOWS 7.

Can I kindly get help regarding this.

OS under consideration is 64-bit Win7 & 32-bit Win XP.

codeLover
  • 3,720
  • 10
  • 65
  • 121

7 Answers7

4

You could get the Operating System 's friendly name by using WMI.

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem");
String operatingSystem = String.Empty;

foreach (ManagementObject query in searcher.Get())
{
   operatingSystem = query["Caption"].ToString();
   break;
}

You could use WMI Code Creator, a great tool from Microsoft to generate WMI queries.

Vijay Sirigiri
  • 4,653
  • 29
  • 31
3

You should be able to get all the information you need from the Environment class, specifically, the OSVersion and Is64BitOperatingSystem properties.

casperOne
  • 73,706
  • 19
  • 184
  • 253
2

Yes all give you right direction Environment.OSVersion gives you OS version, but how to know if its windows XP or 7

You need to compare for versions, here is concerned list

Windows XP 5.1.2600 Current SP3

Windows XP Professional x64 Edition 5.2.3790

Windows Vista 6.0.6000 Current Version changed to 6.0.6002 with SP2

Windows 7 6.1.7600

More Windows OS Version Numbers

if (Environment.OSVersion.Version.ToString().Equals("5.1.2600"))
{
    // windows xp 32-Bit with service pack 3
}
else if (Environment.OSVersion.Version.ToString().Equals("  6.1.7600"))
{
    // windows 7
}
Waqas Raja
  • 10,802
  • 4
  • 33
  • 38
  • Irrespective of many versions I just want to check if the OS is 32 bit WINDOWS XP or 64-bit WINDOWS 7. I dont want to get into versioning details. Can I get help regarding this plz. – codeLover Feb 16 '11 at 13:36
  • @engineerMaster yes you can compare versions to get to know OS is XP or Windows 7 – Waqas Raja Feb 16 '11 at 13:46
  • Well 7 is 6.1 (major version 6, minor version 1) and XP is 5.1.. see here Though you'll need to parse the result and get rid of the build it seems but if you don't want to include Server 2008 R2 you'll need to check the product type as well (no idea how to do that in .NET). – Voo Feb 16 '11 at 13:48
1

Have a look on the System.Environment.OSVersion property. It is of type OperatingSystem which should contain all relevant information.

Sören
  • 2,661
  • 2
  • 19
  • 22
0

You should check out Environment.OSVersion and Environment.Is64BitOperatingSystem.

Though you'll need to manually map the returned information to an appropriate string.

LukeH
  • 263,068
  • 57
  • 365
  • 409
0

Take a look at the Environment class. It contains methods for what you need.

Short Answer:

Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString());
MrBliz
  • 5,830
  • 15
  • 57
  • 81
0

You can get the OS Name from registry.

string productName = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName", "").ToString(); Console.WriteLine(productName);

Output: //For my windows 10 i got

Windows 10 Enterprise

Try this for getting the Windows product name

Pramod
  • 1