Define the operating system and processor type in C #

I want to check what type of operating system I use and which processor. it should be a runtime check. I tried to use

System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") 

and

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

but this is just the environment in which VS works.
I was told to use WMI to test this, but I cannot figure out how to do this. can someone help me with this?

+8
c # operating-system wmi
source share
4 answers

Yes WMI is the best way to do this stuff. You can use this to get OS information:

 ManagementObjectSearcher objMOS = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_OperatingSystem"); 
+5
source share

Getting information about the OS:

 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(); 

Getting information about the CPU:

 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( " ", " " ); 
+23
source share

To determine the operating system, use this code:

 string OPSystemVersion = Environment.OSVersion.ToString(); 

To determine the name and type of CPU, first add the System.Management link to your project, you can use this code:

 public static string SendBackProcessorName() { ManagementObjectSearcher mosProcessor = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"); string Procname = null; foreach (ManagementObject moProcessor in mosProcessor.Get()) { if (moProcessor["name"] != null) { Procname = moProcessor["name"].ToString(); } } return Procname; } 
+2
source share

Take a look at the ManagementClass class: http://msdn.microsoft.com/en-us/library/system.management.managementclass.aspx

 var mgmt = new ManagementClass("Win32_OperatingSystem"); foreach (ManagementObject mgmtObj in mgmt.GetInstances()) { // Just get first value. return mgmtObj[info.Information].ToString().Trim(); } 
0
source share

All Articles