Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear Friends,

I would like to know how to find USB Modems through query.

I have written the WMIquery:
C#
var usbdrives = new ManagementObjectSearcher("SELECT DeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'");


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
Posted
Updated 1-Dec-12 2:10am
v3

WMI will contain all the information you need in the Win32_POTSModem class. In C# or .Net, you can utilize the System.Management namespace to query WMI.

Within .Net, you can use MgmtclassGen.EXE from the platform SDK to generate a class object representing the WMI class.

The command line would be like this:
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:
C#
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);
    }
  }
}
 
Share this answer
 
Comments
Anil Honey 206 28-Aug-12 1:32am    
But it will get all modem names.But i want one the Modem which i inserted
NaveedAhmed 1-Dec-12 8:04am    
you can get available ports by using.. the following code:


// Get a list of serial port names.
string[] ports = SerialPort.GetPortNames();

foreach (string port in ports)
{
listBox1.Items.Add(port);
}
Ilija Saveski 5-Dec-20 3:48am    
Code is Good.
NaveedAhmed 1-Dec-12 8:05am    
thanx buddy for the information, but how could we find the port???
waiting for your answer..
Member 11068274 9-Sep-14 7:37am    
the System.Management is replaced with something like System.Management.Instrumention i guess but there is no way i could find the members of System.Management used in the generated code...getting 38 errors...Plz help me out.
C#
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;
 
Share this answer
 
try...this to get port

C#
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);
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900