VS2012的注释与反注释快捷键
注释是(Ctrl+K, Ctrl+C),实际操作,按住Ctrl键不放,先按K键,再按C键。相当于Ctrl+K加 Ctrl+C的组合键。
反注释是(Ctrl+K, Ctrl+U)。
//调试打印语句
Debug.WriteLine(indata);获得可用的串口
1. //获取所有的串口
2. string[] portlist = SerialPort.GetPortNames();
3. for (int i = 0; i < portlist.Length; i++)
4. {
5. this.comboBox1.Items.Add(portlist[i]);
6. }
2.这个方法会出现“未将对象引用设置到对象的实例”,问题出在哪里了呢?
这种方法没行通!
1. RegistryKey keycom = Registry.LocalMachine.OpenSubKey("Hardware\\DevinceMap\\SerialComm");
2. try
3. {
4.
5. string[] subkeys = keycom.GetValueNames();
6.
7. string[] str_key = new string[subkeys.Length];
8. for (int i = 0; i < subkeys.Length; i++)
9. {
10. this.comboBox1.Items.Add(str_key[i]);
11. }
12. }
13. catch (Exception ex)
14. {
15. MessageBox.Show(ex.Message);
16. return;
17. }
3.错误 1 未能找到类型或命名空间名称“ManagementClass" 解决(VS2012)
这个主要是因为未添加引用,
选中工程,右键,添加引用选项,出现引用管理器对话框,如图

选中缺少的引用。
4.一段获得CPU硬件信息的代码
1. using System;
2. using System.Collections.Generic;
3. using System.ComponentModel;
4. using System.Data;
5. using System.Drawing;
6. using System.Linq;
7. using System.Text;
8. using System.Threading.Tasks;
9. using System.Windows.Forms;
10. using System.Management;
11.
12. namespace test
13. {
14. public partial class Form1 : Form
15. {
16. public Form1()
17. {
18. InitializeComponent();
19. }
20.
21. public void GetInfo()
22. {
23. string cpuInfo = "";//cpu序列号
24. new ManagementClass("Win32_Processor");
25. ManagementObjectCollection moc = cimobject.GetInstances();
26. foreach (ManagementObject mo in moc)
27. {
28. "ProcessorId"].Value.ToString();
29.
30. this.richTextBox1.AppendText("cpu序列号:" + cpuInfo.ToString() + "\r\n ");
31.
32. }
33. //获取硬盘ID
34. String HDid;
35. new ManagementClass("Win32_DiskDrive");
36. ManagementObjectCollection moc1 = cimobject1.GetInstances();
37. foreach (ManagementObject mo in moc1)
38. {
39. string)mo.Properties["Model"].Value;
40.
41. this.richTextBox1.AppendText("硬盘序列号:" + HDid.ToString() + "\r\n ");
42. }
43. //获取网卡硬件地址
44. new ManagementClass("Win32_NetworkAdapterConfiguration");
45. ManagementObjectCollection moc2 = mc.GetInstances();
46. foreach (ManagementObject mo in moc2)
47. {
48. if ((bool)mo["IPEnabled"] == true)
49.
50. this.richTextBox1.AppendText("MAC address\t{0}" + mo["MacAddress"].ToString() + "\r\n ");
51. mo.Dispose();
52. }
53. }
54.
55. public string GetSerialPort()
56. {
57. string strPortInfo = " ";
58. new ManagementClass("Win32_SerialPort");
59. ManagementObjectCollection moc = mc.GetInstances();
60. foreach (ManagementObject mo in moc)
61. {
62. "Name"].ToString() + "\r\n ";
63.
64. }
65.
66. return strPortInfo;
67. }
68.
69. private void button1_Click(object sender, EventArgs e)
70. {
71. string str = GetSerialPort();
72. // this.richTextBox1.AppendText("dddd");
73. this.richTextBox1.AppendText(str);
74. GetInfo();
75. }
76. }
77. }
代码已测试,结果如图

5.C#获得计算机的硬件配置
1. ----------------------------------------------------
2. 最后给你附上一段程序
3. /* **********************************************
4. * Rainsoft Development Library for Microsoft.NET
5. *
6. * Copyright (c) 2004,2005 RainTrail Studio.China
7. * All Rigths Reserved!
8. * Author: Q.yuhen (qyuhen@hotmail.com)
9. ********************************************** */
10. using System;
11. using System.Management;
12. using System.Collections;
13. using System.Collections.Specialized;
14. using System.Text;
15.
16. namespace Rainsoft.Management
17. {
18. #region WMIPath
19. public enum WMIPath
20. {
21. // 硬件
22. Win32_Processor, // CPU 处理器
23. Win32_PhysicalMemory, // 物理内存条
24. Win32_Keyboard, // 键盘
25. Win32_PointingDevice, // 点输入设备,包括鼠标。
26. Win32_FloppyDrive, // 软盘驱动器
27. Win32_DiskDrive, // 硬盘驱动器
28. Win32_CDROMDrive, // 光盘驱动器
29. Win32_BaseBoard, // 主板
30. Win32_BIOS, // BIOS 芯片
31. Win32_ParallelPort, // 并口
32. Win32_SerialPort, // 串口
33. Win32_SerialPortConfiguration, // 串口配置
34. Win32_SoundDevice, // 多媒体设置,一般指声卡。
35. Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP)
36. Win32_USBController, // USB 控制器
37. Win32_NetworkAdapter, // 网络适配器
38. Win32_NetworkAdapterConfiguration, // 网络适配器设置
39. Win32_Printer, // 打印机
40. Win32_PrinterConfiguration, // 打印机设置
41. Win32_PrintJob, // 打印机任务
42. Win32_TCPIPPrinterPort, // 打印机端口
43. Win32_POTSModem, // MODEM
44. Win32_POTSModemToSerialPort, // MODEM 端口
45. Win32_DesktopMonitor, // 显示器
46. Win32_DisplayConfiguration, // 显卡
47. Win32_DisplayControllerConfiguration, // 显卡设置
48. Win32_VideoController, // 显卡细节。
49. Win32_VideoSettings, // 显卡支持的显示模式。
50.
51. // 操作系统
52. Win32_TimeZone, // 时区
53. Win32_SystemDriver, // 驱动程序
54. Win32_DiskPartition, // 磁盘分区
55. Win32_LogicalDisk, // 逻辑磁盘
56. Win32_LogicalDiskToPartition, // 逻辑磁盘所在分区及始末位置。
57. Win32_LogicalMemoryConfiguration, // 逻辑内存配置
58. Win32_PageFile, // 系统页文件信息
59. Win32_PageFileSetting, // 页文件设置
60. Win32_BootConfiguration, // 系统启动配置
61. Win32_ComputerSystem, // 计算机信息简要
62. Win32_OperatingSystem, // 操作系统信息
63. Win32_StartupCommand, // 系统自动启动程序
64. Win32_Service, // 系统安装的服务
65. Win32_Group, // 系统管理组
66. Win32_GroupUser, // 系统组帐号
67. Win32_UserAccount, // 用户帐号
68. Win32_Process, // 系统进程
69. Win32_Thread, // 系统线程
70. Win32_Share, // 共享
71. Win32_NetworkClient, // 已安装的网络客户端
72. Win32_NetworkProtocol, // 已安装的网络协议
73. }
74. #endregion
75.
76. /// <summary>
77. /// 获取系统信息
78. /// </summary>
79. /// <example>
80. /// <code>
81. /// WMI w = new WMI(WMIPath.Win32_NetworkAdapterConfiguration);
82. /// for (int i = 0; i < w.Count; i ++)
83. /// {
84. /// if ((bool)w[i, "IPEnabled"])
85. /// {
86. /// Console.WriteLine("Caption:{0}", w[i, "Caption"]);
87. /// Console.WriteLine("MAC Address:{0}", w[i, "MACAddress"]);
88. /// }
89. /// }
90. /// </code>
91. /// </example>
92. public sealed class WMI
93. {
94. private ArrayList mocs;
95. private StringDictionary names; // 用来存储属性名,便于忽略大小写查询正确名称。
96.
97. /// <summary>
98. /// 信息集合数量
99. /// </summary>
100. public int Count
101. {
102. get { return mocs.Count; }
103. }
104.
105. /// <summary>
106. /// 获取指定属性值,注意某些结果可能是数组。
107. /// </summary>
108. public object this[int index, string propertyName]
109. {
110. get
111. {
112. try
113. {
114. string trueName = names[propertyName.Trim()]; // 以此可不区分大小写获得正确的属性名称。
115. Hashtable h = (Hashtable)mocs[index];
116. return h[trueName];
117. }
118. catch
119. {
120. return null;
121. }
122. }
123. }
124.
125. /// <summary>
126. /// 返回所有属性名称。
127. /// </summary>
128. /// <param name="index"></param>
129. /// <returns></returns>
130. public string[] PropertyNames(int index)
131. {
132. try
133. {
134. Hashtable h = (Hashtable)mocs[index];
135. string[] result = new string[h.Keys.Count];
136.
137. h.Keys.CopyTo(result, 0);
138.
139. Array.Sort(result);
140. return result;
141. }
142. catch
143. {
144. return null;
145. }
146. }
147.
148. /// <summary>
149. /// 返回测试信息。
150. /// </summary>
151. /// <returns></returns>
152. public string Test()
153. {
154. try
155. {
156. StringBuilder result = new StringBuilder(1000);
157.
158. for (int i = 0; i < Count; i++)
159. {
160. int j = 0;
161. foreach(string s in PropertyNames(i))
162. {
163. result.Append(string.Format("{0}:{1}={2}\n", ++j, s, this[i, s]));
164.
165. if (this[i, s] is Array)
166. {
167. Array v1 = this[i, s] as Array;
168. for (int x = 0; x < v1.Length; x++)
169. {
170. result.Append("\t" + v1.GetValue(x) + "\n");
171. }
172. }
173. }
174. result.Append("======WMI=======\n");
175. }
176.
177. return result.ToString();
178. }
179. catch
180. {
181. return string.Empty;
182. }
183. }
184.
185. /// <summary>
186. /// 构造函数
187. /// </summary>
188. /// <param name="path"></param>
189. public WMI(string path)
190. {
191. names = new StringDictionary();
192. mocs = new ArrayList();
193.
194. try
195. {
196. ManagementClass cimobject = new ManagementClass(path);
197. ManagementObjectCollection moc = cimobject.GetInstances();
198.
199. bool ok = false;
200. foreach(ManagementObject mo in moc)
201. {
202. Hashtable o = new Hashtable();
203. mocs.Add(o);
204.
205. foreach (PropertyData p in mo.Properties)
206. {
207. o.Add(p.Name, p.Value);
208. if (!ok) names.Add(p.Name, p.Name);
209. }
210.
211. ok = true;
212. mo.Dispose();
213. }
214. moc.Dispose();
215. }
216. catch(Exception e)
217. {
218. throw new Exception(e.Message);
219. }
220. }
221.
222. /// <summary>
223. /// 构造函数
224. /// </summary>
225. /// <param name="path"></param>
226. public WMI(WMIPath path): this(path.ToString())
227. {
228. }
229. }
230. }
网址在浏览器中打开:
System.Diagnostics.Process.Start("IExplore.exe", "http://www.xyjzvip.icoc.cc/");
System.Diagnostics.Process.Start(@"http://www.xyjzvip.icoc.cc/"); // call default browser
















