本文介绍了如何编写WMI查询以在C#中查找USB调制解调器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的朋友,
我想知道如何通过查询找到USB调制解调器.
我已经编写了WMIquery:
Dear Friends,
I would like to know how to find USB Modems through query.
I have written the WMIquery:
var usbdrives = new ManagementObjectSearcher("SELECT DeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'");
这适用于USB PenDrives,但是我需要找到调制解调器.
如何编写USB调制解调器的WMI查询?
如果有任何有关此方面的信息,请与我分享.
问候,
AnilKumar.D
This is working for USB PenDrives, but I need to find the modems.
How to write WMI query for USB Modems?
If any information regarding this please share with me.
Regards,
AnilKumar.D
推荐答案
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\mgmtclassgen.exe Win32_POTSModem /L CS /P c:\POTSModem\Win32_POTSModem.cs
然后可以在代码中使用它:
and then you can use that in your code:
using System;
using System.Collections.Generic;
using System.Management;
using ROOT.CIMV2.Win32;
public class MyClass
{
public static void Main()
{
foreach (POTSModem modem in POTSModem.GetInstances()) {
Console.WriteLine(modem.Description);
}
}
}
string modems = "";
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_POTSModem ");
foreach (ManagementObject queryObj in searcher.Get())
{
if ((string)queryObj["Status"] == "OK")
{
listBox1.Items.Add(queryObj["AttachedTo"] + " - " + System.Convert.ToString(queryObj["Description"]));
}
}
return modems;
using System;
using System.Management;
using System.Windows.Forms;
namespace WMISample
{
public class MyWMIQuery
{
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_POTSModem WHERE AttachedTo = 'COM7'");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_POTSModem instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("AttachedTo: {0}", queryObj["AttachedTo"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
}
}
}
这篇关于如何编写WMI查询以在C#中查找USB调制解调器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!