Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine operating system and processor type in C#

I want to check what type of operating system i use and what kind of processor. this should be check on run time. i tried using

System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")

and

System.OperatingSystem osInfo2 = System.Environment.OSVersion;
Console.WriteLine(osInfo2.ToString());

but it's just the enviroment that VS is running on.
I was told to use WMI to check it but i can't find out how. can someone help me with that?

like image 726
Ben2307 Avatar asked Aug 04 '11 16:08

Ben2307


People also ask

How do I identify my operating system?

Android DevicesGo to the home screen of your device. Touch "Settings," then touch "About Phone" or "About Device." From there, you can find the Android version of your device.

Which operating system is used in C?

C was invented to write an operating system called UNIX. C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute (ANSI). The UNIX OS was totally written in C.

How you can check whether a machine is 64-bit or 32-bit in C?

Click Start, type system in the search box, and then click System in the Control Panel list. The operating system is displayed as follows: For a 64-bit version operating system: 64-bit Operating System appears for the System type under System.

How do I check C System?

Software Engineering C To check the operating system of the host in a C or C++ code, we need to check the macros defined by the compiler (GNU GCC or G++). For example, on Windows platform, the compiler has a special macro named _WIN32 defined. So, if the macro _WIN32 is defined, we are on Windows.


2 Answers

Retrieving OS info:

var wmi =
    new ManagementObjectSearcher( "select * from Win32_OperatingSystem" )
    .Get()
    .Cast<ManagementObject>()
    .First();

OS.Name = ((string)wmi["Caption"]).Trim();
OS.Version = (string)wmi["Version"];
OS.MaxProcessCount = (uint)wmi["MaxNumberOfProcesses"];
OS.MaxProcessRAM = (ulong)wmi["MaxProcessMemorySize"];
OS.Architecture = (string)wmi["OSArchitecture"];
OS.SerialNumber = (string)wmi["SerialNumber"];
OS.Build = ((string)wmi["BuildNumber"]).ToUint();

Retrieving CPU info:

var cpu =
    new ManagementObjectSearcher( "select * from Win32_Processor" )
    .Get()
    .Cast<ManagementObject>()
    .First();

CPU.ID = (string)cpu["ProcessorId"];
CPU.Socket = (string)cpu["SocketDesignation"];
CPU.Name = (string)cpu["Name"];
CPU.Description = (string)cpu["Caption"];
CPU.AddressWidth = (ushort)cpu["AddressWidth"];
CPU.DataWidth = (ushort)cpu["DataWidth"];
CPU.Architecture = (CPU.CpuArchitecture)(ushort)cpu["Architecture"];
CPU.SpeedMHz = (uint)cpu["MaxClockSpeed"];
CPU.BusSpeedMHz = (uint)cpu["ExtClock"];
CPU.L2Cache = (uint)cpu["L2CacheSize"] * (ulong)1024;
CPU.L3Cache = (uint)cpu["L3CacheSize"] * (ulong)1024;
CPU.Cores = (uint)cpu["NumberOfCores"];
CPU.Threads = (uint)cpu["NumberOfLogicalProcessors"];

CPU.Name =
   CPU.Name
   .Replace( "(TM)", "™" )
   .Replace( "(tm)", "™" )
   .Replace( "(R)", "®" )
   .Replace( "(r)", "®" )
   .Replace( "(C)", "©" )
   .Replace( "(c)", "©" )
   .Replace( "    ", " " )
   .Replace( "  ", " " );
like image 154
treetey Avatar answered Sep 30 '22 18:09

treetey


Yes WMI is the best way to do this kind of stuff You can use this to retrieve OS informations :

ManagementObjectSearcher objMOS = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM  Win32_OperatingSystem");
like image 32
mmorel Avatar answered Sep 30 '22 20:09

mmorel