C# 获得本地通用网卡信息

可以通过使用命名空间下的ManagementObjectSearcher类及其方法Get来获得通用网卡信息。

其中,最重要的是ManagementObjectSearcher构造函数的输入参数,可以传入以下的QueryString。

SELECT * FROM Win32_NetworkAdapterConfiguration

以下的例子为将本地网卡的配置信息写入一个文件并保存,代码如下:

 1 using System.IO;
 2 using System.Linq;
 3 using System.Management;
 4 
 5 namespace ConsoleApp
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration") ;
12             ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
13             StreamWriter writer = new StreamWriter("result.csv");
14             bool isFirstRow = true;
15             foreach (ManagementObject managementObject in managementObjectCollection)
16             {
17                 string header = string.Empty;
18                 string content = string.Empty;
19                 string separator1 = ",";
20                 string separator2 = " | ";
21                 foreach (var item in managementObject.Properties)
22                 {
23                     if (isFirstRow)
24                     {
25                         header = $"{header}{separator1}{item.Name}";
26                     }
27                     string valueTemp = string.Empty;
28                     if (item.Value != null)
29                     {
30                         if (item.Value is string[]  || item.Value is ushort[])
31                         {
32                             string[] valuesStrTemp;
33                             if(item.Value is ushort[])
34                                 valuesStrTemp = ((ushort[])item.Value).Select(x => x.ToString()).ToArray();
35                             else
36                                 valuesStrTemp = item.Value as string[];
37 
38                             for (int i = 0; i < valuesStrTemp.Length; i++)
39                             {
40                                 valueTemp = $"{valueTemp}{separator2}{valuesStrTemp[i]}";
41                             }
42                             if(valuesStrTemp.Length != 0)
43                                 valueTemp = valueTemp.Remove(0, separator2.Length);
44                         }
45                         else
46                             valueTemp = item.Value.ToString();
47                     }
48                     content = $"{content}{separator1}{valueTemp}";
49                 }
50                 if (isFirstRow)
51                 {
52                     header = header.Remove(0, separator1.Length);
53                     writer.WriteLine(header);
54                     isFirstRow = false;
55                 }
56                 content = content.Remove(0, separator1.Length);
57                 writer.WriteLine(content);
58             }
59             writer.Close();
60         }
61     }
62 }

运行结果如下:

posted @ 2019-07-31 13:53 cnc 阅读(...) 评论(...) 编辑 收藏
著作版权归 www.cnc6.cn 所有   粤ICP备17062562号