Getting list of modems or dongle attached to pc

Getting list of modems attached to pc in c#.net windows application
  1. Right Click on references click Add reference.
  2. Add reference of Assembly  "System.Management"
  3. Create new form "frmModems"
  4. Add ListView "lstDevicList" to frmModems
Output


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
using System.IO.Ports;
using System.IO;
namespace WindowsFormsApplication1
{
    public partial class frmModems : Form
    {
        public frmModems()
        {
            InitializeComponent();
        }

        private void btnGetList_Click(object sender, EventArgs e)
        {
            getPorts();
        }

        private void getPorts()
        {
           
                try
                {

                    ManagementObjectSearcher obj = new ManagementObjectSearcher("root\\CIMV2", "Select * from Win32_POTSModem");
                    lstDevicList.Items.Clear();
                    foreach (ManagementBaseObject sp in obj.Get())
                    {

                        try
                        {
                            if (sp["STATUS"].ToString().Trim().ToLower() == "ok")
                            {
                                ListViewItem itm = new ListViewItem(sp["Caption"].ToString());
                                itm.SubItems.Add(sp["STATUS"].ToString());
                                itm.SubItems.Add(sp["DESCRIPTION"].ToString());
                                itm.SubItems.Add(sp["MaxBaudRateToSerialPort"].ToString());
                                itm.SubItems.Add(sp["AttachedTo"].ToString());
                                lstDevicList.Items.Add(itm);
                            }

                        }
                        catch { }
                    }

                }
                catch
                {

                }
            
        }
  
    }
}


Post a Comment