开发者

How to get the name of OS in which the code is running using C#?

I have a C# application开发者_C百科 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.


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.


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


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
}


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


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

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


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

Short Answer:

Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString());


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜