在下面的方法中,我启动了一个调用WMIC.exe的进程,以便查询附加的DeviceID设备的DeviceID。问题是,尽管设备是插入的,但WIMC不返回实例,就好像设备没有被插入一样。然而,设备管理器同时在“端口(COM& LPT)”下显示设备,这意味着WMIC的信息不准确。我的意思是,如果设备已经崩溃,或者以任何方式出现故障,并且它需要某种重置,那么它也不应该出现在设备管理器列表中。
方法:
private string DonglePortName()
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Windows\\System32\\wbem\\WMIC.exe";
startInfo.Arguments = "PATH Win32_SerialPort GET /VALUE";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process processTemp = new Process();
processTemp.StartInfo = startInfo;
processTemp.EnableRaisingEvents = true;
processTemp.Start();
string output = processTemp.StandardOutput.ReadToEnd();
int indexOfBGEntry = output.IndexOf("Bluegiga Bluetooth Low Energy");
if(indexOfBGEntry > -1)
{
string output_sub = output.Substring(indexOfBGEntry);
string str = "DeviceID=";
int i = output_sub.IndexOf(str);
int start = i + str.Length;
string substr = output_sub.Substring(start);
int end = substr.IndexOf("\r");
return output_sub.Substring(start, end);
}
return null;
}此时,在cmd窗口上执行:
C:\\Windows\\System32\\wbem\\WMIC.exe PATH Win32_SerialPort GET /VALUE
返回:
No Instance(s) Available.同时,设备管理器显示设备(即使在单击扫描以查找硬件更改之后)。
因此,上面的C#代码实际上并不需要演示这个问题,但我只想在这里使用它,以防有人可以提出另一种更健壮的获取设备端口的方法(在C#中),或者指出WMIC不准确的原因。
另一条线索是,WMIC总是在代码的另一部分出现异常(和崩溃)后返回No Instance(s) Available,该代码是对SerialPort进行读写的。然后,我不得不拔掉和回复usb设备,然后它又被WMIC“查看”了。尽管如此,设备管理器在任何时候都会显示设备的插头。
发布于 2015-02-06 08:08:35
您查看的是错误的WMIC路径,因为Win32_SerialPort WMI类表示运行Win32_SerialPort的计算机系统上的串行端口。简而言之,下面是ProviderType属性(通信提供者类型)列表:
传真设备,LAT协议,调制解调器设备,网络桥,并行口,RS232串口,RS422端口,RS423端口,RS449端口,扫描仪设备,TCP/IP TelNet,X.25,未指定
检查下一个命令输出,而不是:
wmic path CIM_LogicalDevice where "Description like 'USB%'" get /value资源:计算机系统硬件类
https://stackoverflow.com/questions/28352010
复制相似问题