C # obtain information about the operating system,

Source: Internet
Author: User
Tags osclass

C # obtain information about the operating system,

This article uses a Demo to explain how to use C # to obtain information about the operating system, such as the memory size, CPU size, machine name, environment variables, and other operating system software and hardware.

Knowledge points involved:

  • Environment provides information about the current Environment and platform and the methods for operating them.
  • ManagementClass indicates the CIM management class. The management class is a WMI class, such as Win32_LogicalDisk and Win32_Process. The former indicates the disk drive, and the latter indicates the process (such as Notepad.exe ). Members of this class can access WMI data using specific WMI class paths.

As follows:

System Information:Obtain information such as the system directory, platform ID, logon user name, drive letter, and domain.

Environment variable:That is, the operating system running parameters to see if there is any bright Information

Special directory:Is my desktop, my documents, favorites, and other directories very familiar?

Operating System:The following information is used to obtain information about the CPU, such as the model, name, number, speed, and vendor. You can also obtain other information such as memory and hard disk]

The Code is as follows:

1 namespace DemoEnvironment 2 {3 public partial class MainFrom: Form 4 {5 public MainFrom () 6 {7 InitializeComponent (); 8} 9 10 private void MainFrom_Load (object sender, EventArgs e) 11 {12 string machineName = Environment. machineName; 13 string osVersionName = GetOsVersion (Environment. OSVersion. version); 14 string servicePack = Environment. OSVersion. servicePack; 15 osVersionName = osVersionN Ame + "" + servicePack; 16 string userName = Environment. userName; 17 string domainName = Environment. userDomainName; 18 string tickCount = (Environment. tickCount/1000 ). toString () + "s"; 19 string systemPageSize = (Environment. systemPageSize/1024 ). toString () + "KB"; 20 string systemDir = Environment. systemDirectory; 21 string stackTrace = Environment. stackTrace; 22 string processorCounter = E Ronment. processorCount. toString (); 23 string platform = Environment. OSVersion. platform. toString (); 24 string newLine = Environment. newLine; 25 bool is64Os = Environment. is64BitOperatingSystem; 26 bool is64Process = Environment. is64BitProcess; 27 28 string currDir = Environment. currentDirectory; 29 string export line = Environment. commandLine; 30 string [] drives = Environment. getLogicalDrives (); 31 // Long workingSet = (Environment. worker set/1024); 32 this. lblMachineName. text = machineName; 33 this. lblOsVersion. text = osVersionName; 34 this. lblUserName. text = userName; 35 this. lblDomineName. text = domainName; 36 this. lblStartTime. text = tickCount; 37 this. lblPageSize. text = systemPageSize; 38 this. lblSystemDir. text = systemDir; 39 this. lblLogical. text = string. join (",", drives); 40 this. lb LProcesserCounter. Text = processorCounter; 41 this. lblPlatform. Text = platform; 42 this. lblNewLine. Text = newLine. ToString (); 43 this. lblSystemType. Text = is64Os? "64bit": "32bit"; 44 this. lblProcessType. Text = is64Process? "64bit": "32bit"; 45 this. lblCurDir. text = currDir; 46 this. lbl1_line. text = plain line; 47 this. lblWorkSet. text = GetPhisicalMemory (). toString () + "MB"; 48 // Environment variable 49 // HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Control \ Session Manager \ Environment 50 IDictionary dicMachine = Environment. getEnvironmentVariables (EnvironmentVariableTarget. machine); 51 this. rtbVaribles. appendText (string. format ("{0 }:{ 1} "," Machine environment variable ", newLine); 52 foreach (string str in dicMachine. keys) {53 string val = dicMachine [str]. toString (); 54 this. rtbVaribles. appendText (string. format ("{0 }:{ 1} {2}", str, val, newLine); 55} 56 this. rtbVaribles. appendText (string. format ("{0} {1 }", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>> ", newLine); 57 // The Environment variables are stored in or retrieved from the HKEY_CURRENT_USER \ Environment item in the Windows operating system registry. 58 IDictionary dicUser = Environment. getEnvironmentVariables (EnvironmentVariableTarget. user); 59 this. rtbVaribles. appendText (string. format ("{0 }:{ 1}", "user environment variable", newLine); 60 foreach (string str in dicUser. keys) 61 {62 string val = dicUser [str]. toString (); 63 this. rtbVaribles. appendText (string. format ("{0 }:{ 1} {2}", str, val, newLine); 64} 65 this. rtbVaribles. appendText (string. format ("{0} {1 }"," >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>> ", NewLine); 66 IDictionary dicProcess = Environment. getEnvironmentVariables (EnvironmentVariableTarget. process); 67 this. rtbVaribles. appendText (string. format ("{0 }:{ 1}", "process environment variable", newLine); 68 foreach (string str in dicProcess. keys) 69 {70 string val = dicProcess [str]. toString (); 71 this. rtbVaribles. appendText (string. format ("{0 }:{ 1} {2 }", Str, val, newLine); 72} 73 // special directory 74 string [] names = Enum. getNames (typeof (Environment. specialFolder); 75 foreach (string name in names) {76 77 Environment. specialFolder sf; 78 if (Enum. tryParse <Environment. specialFolder> (name, out sf) 79 {80 string folder = Environment. getFolderPath (sf); 81 this. rtbFolders. appendText (string. format ("{0 }:{ 1} {2}", name, folder, newLine); 82} 83} 84 // obtain other hardware, Software information 85 GetPhicnalInfo (); 86} 87 88 private string GetOsVersion (Version ver) {89 string strClient = ""; 90 if (ver. major = 5 & ver. minor = 1) 91 {92 strClient = "Win XP"; 93} 94 else if (ver. major = 6 & ver. minor = 0) 95 {96 strClient = "Win Vista"; 97} 98 else if (ver. major = 6 & ver. minor = 1) 99 {100 strClient = "Win 7"; 101} 102 else if (ver. major = 5 & ver. minor = 0) 103 {104 StrClient = "Win 2000"; 105} 106 else107 {108 strClient = "unknown"; 109} 110 return strClient; 111} 112 113 // <summary> 114 // obtain the system memory size 115 // </summary> 116 // <returns> memory size (in MB) </returns> 117 private int GetPhisicalMemory () 118 {119 ManagementObjectSearcher searcher = new ManagementObjectSearcher (); // query the system information management object 120 searcher. query = new SelectQuery ("Win32_PhysicalMemory", "", new string [] {"Ca Pacity "}); // set the query condition 121 ManagementObjectCollection collection = searcher. get (); // Get memory capacity 122 ManagementObjectCollection. managementObjectEnumerator em = collection. getEnumerator (); 123 124 long capacity = 0; 125 while (em. moveNext () 126 {127 ManagementBaseObject baseObj = em. current; 128 if (baseObj. properties ["Capacity"]. value! = Null) 129 {130 try131 {132 capacity + = long. parse (baseObj. properties ["Capacity"]. value. toString (); 133} 134 catch135 {136 return 0; 137} 138} 139 return (int) (capacity/140 ); 141} 142 143 // <summary> 144 /// https://msdn.microsoft.com/en-us/library/aa394084 (VS.85 ). aspx145 // </summary> 146 // <returns> </returns> 147 private int GetPhicnalInfo () {148 ManagementClass osClass = new ManagementClass ("Win32_Processor "); // you can try the following methods for unexpected gains. // Win32_PhysicalMemory/Win32_Keyboard/Win32_ComputerSystem/Win32_OperatingSystem149 foreach (ManagementObject obj in osClass. getInstances () 150 {151 PropertyDataCollection pdc = obj. properties; 152 foreach (PropertyData pd in pdc) {153 this. rtbOs. appendText (string. format ("{0 }:{ 1} {2}", pd. name, pd. value, "\ r \ n"); 154} 155} 156 return 0; 157} 158} 159}
View Code


Project download

Small examples: small knowledge, long steps, and even thousands of miles, small streams are built into rivers and seas.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.