111,119
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace ConsoleApplication2
{
class Program
{
public static string[] MulGetHardwareInfo(string hardType, string propKey)
{
List<string> strs = new List<string>();
try
{
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType))
{
var hardInfos = searcher.Get();
foreach (var hardInfo in hardInfos)
{
strs.Add(hardInfo.Properties[propKey].Value.ToString());
}
searcher.Dispose();
}
return strs.ToArray();
}
catch
{
return null;
}
finally
{ strs = null; }
}
static void Main(string[] args)
{
string[] ss = MulGetHardwareInfo("Win32_USBHub", "DeviceID");
for (int i = 0; i < ss.Length; i++)
{
Console.WriteLine(ss[i]);
}
}
}
}



[/quote]
这个工具和2楼的兄弟的工具类似的,谢谢[/quote]
这是Windows系统自带的,在运行里输入wbemtest就行了,先用这个查一下你要查看的信息包含在哪个属性,然后代码实现。
例如这个查端口的函数:
/// WMI取硬件信息
/// </summary>
/// <param name="hardType"></param>
/// <param name="propKey"></param>
/// <returns></returns>
public static string[] MulGetHardwareInfo(HardwareEnum hardType, string propKey)
{
List<string> strs = new List<string>();
try
{
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType))
{
var hardInfos = searcher.Get();
foreach (var hardInfo in hardInfos)
{
if (hardInfo.Properties[propKey].Value.ToString().Contains("COM"))
{
strs.Add(hardInfo.Properties[propKey].Value.ToString());
}
}
searcher.Dispose();
}
return strs.ToArray();
}
catch
{
return null;
}
finally
{ strs = null; }
}
//通过WMI获取COM端口
string[] ss = MulGetHardwareInfo(HardwareEnum.Win32_PnPEntity, "Name");
[/quote]
/// <param name="hardType"></param>
/// <param name="propKey"></param>
hardType和propKey是什么呢
[/quote]
这个工具和2楼的兄弟的工具类似的,谢谢[/quote]
这是Windows系统自带的,在运行里输入wbemtest就行了,先用这个查一下你要查看的信息包含在哪个属性,然后代码实现。
例如这个查端口的函数:
/// WMI取硬件信息
/// </summary>
/// <param name="hardType"></param>
/// <param name="propKey"></param>
/// <returns></returns>
public static string[] MulGetHardwareInfo(HardwareEnum hardType, string propKey)
{
List<string> strs = new List<string>();
try
{
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType))
{
var hardInfos = searcher.Get();
foreach (var hardInfo in hardInfos)
{
if (hardInfo.Properties[propKey].Value.ToString().Contains("COM"))
{
strs.Add(hardInfo.Properties[propKey].Value.ToString());
}
}
searcher.Dispose();
}
return strs.ToArray();
}
catch
{
return null;
}
finally
{ strs = null; }
}
//通过WMI获取COM端口
string[] ss = MulGetHardwareInfo(HardwareEnum.Win32_PnPEntity, "Name");
[/quote]
这个工具和2楼的兄弟的工具类似的,谢谢