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

I have a C # application that is expected to work in both WIn 7 and Win XP. I need to check OS NAME in my C # source code before distributing MSI and EXE to clients.

Without going into finer version control, my code wants to check if it is 32-bit WINDOWS XP or 64-bit WINDOWS 7.

May I get help regarding this.

The OS is seen as a 64-bit version of Win7 and a 32-bit version of Win XP.

+3
source share
6 answers

You can get a friendly operating system name 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;
}

WMI Code Creator, Microsoft WMI.

+4

, . Environment.OSVersion , , Windows XP 7

,

Windows XP 5.1.2600 SP3

Windows XP Professional x64 Edition 5.2.3790

Windows Vista 6.0.6000 6.0.6002 2

Windows 7 6.1.7600

Windows

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
}
+2

Look at the property System.Environment.OSVersion. This is an OperatingSystem type that should contain all relevant information.

+1
source

You must check Environment.OSVersionand Environment.Is64BitOperatingSystem.

Although you need to manually display the returned information in the appropriate line.

0
source

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

Short answer:

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

Source: https://habr.com/ru/post/1792523/


All Articles