Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#
Article

A Vista Wireless Network Scanner

Rate me:
Please Sign up or sign in to vote.
4.51/5 (21 votes)
26 Jun 20072 min read 333.4K   11.6K   85   53
A NetStumbler (sort of) for Vista

Screenshot - WifiScanner1.gif

Introduction

I recently got a new notebook and was routinely transferring all of my old, favourite utilities to it when I found that NetStumbler would not work with Vista. I saw some references to a poor-man's version with the following command line:

netsh wlan show networks mode=bssid

I decided to put it in a window, so here is my effort. For Microsoft documentation on netsh, see Windows Netsh.

The code

A timer on the Form calls the Scan() function every two seconds. The Scan() function first uses the Process() class to execute the netsh command with the command line parameters:

C#
Process proc = new Process();
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = "netsh";
proc.StartInfo.Arguments = "wlan show networks mode=bssid";
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();

The output is then parsed line-by-line to build an array of currently active networks. This is presented in a listView using the MAC Address as the item text with sub-items for the rest of the information. Discovered networks that are not already in the listView are found using:

C#
ListViewItem SearchItem = new ListViewItem();
SearchItem = listView1.FindItemWithText(Networks[i, 0]);
if (SearchItem == null)
{
    // New discovery - add it to the list

Any networks already in the listView are updated. The signal level icon is generated by bitmaps on the listView:

C#
int SignalInt = Convert.ToInt32(Networks[i, 3].TrimEnd(' ').TrimEnd('%'));
if (SignalInt > 50) listView1.Items[listView1.Items.Count-1].ImageIndex = 0;
else . . .

The coloured icons represent the signal levels as:

Green Dot Greater than 50% Signal Level

Yellow Dot 41% to 50% Signal Level

Orange Dot 31% to 40% Signal Level

Red Dot 21% to 30% Signal Level

Dark Red Dot 1% to 20% Signal Level

Grey Dot No Signal

Points of interest

One annoyance with the listView control was flicker. Every Update() or Refresh() caused the whole list to be cleared and re-written. I used a tip from Don Koster and added his listViewNF class. Using this listViewNF class stopped the flicker by redrawing only the changed areas.

C#
class ListViewNF : System.Windows.Forms.ListView
{
    public ListViewNF()
    {
        //Activate double buffering
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer | 
            ControlStyles.AllPaintingInWmPaint, true);

        //Enable the OnNotifyMessage event so we get a chance to filter out
        // Windows messages before they get to the Form's WndProc
        this.SetStyle(ControlStyles.EnableNotifyMessage, true);
    }

    protected override void OnNotifyMessage(Message m)
    {
        //Filter out the WM_ERASEBKGND message
        if (m.Msg != 0x14)
        {
            base.OnNotifyMessage(m);
        }
    }

Known issues

The netsh command can sometimes be slow at updating the real network state. If a network is connected, it seems that the signal level displayed by netsh is not updated. Being connected also seems to alter the signal level of the connected network and other networks. When a connected network is turned off, it can take more than a minute for netsh -- or possibly this is a driver issue -- to remove the network from the output. Turning the wireless off and back on seems to force a new signal level. Also, sometimes the netsh command seems to change signal levels at every update. Once it gets in this mode, it will update the signal level for every command when operating on main power, but if it's operating on battery power it doesn't! The normal signal update interval seems to be one minute. I tested this using a Dell 640m notebook with an Intel 3945ABG wireless adapter.

History

  • June 23, 2007: New article.
  • June 24, 2007: Whoops! Bitmaps were not included in the demo project. This is fixed now.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Ireland Ireland
My first experiences with computing was with a Bendix G15 drum computer at Carnegie Mellon. More followed with CDC G20, IBM 360, Univac 1108, Apple II, and 386XXX. I have done microprocessor programming and design for 6502, Z80, and 8051. I have moved to C# now and am still struggling to understand this. My career has been in engineering starting with Texas Instruments and continuing with Becton Dickinson, Dataproducts, Hitachi Printing Solutions and now retired from Ricoh Printing Systems Europe.

Comments and Discussions

 
QuestionWhat is the last column 'Speed' referring to? Pin
Gustavo19146-Jun-17 3:07
Gustavo19146-Jun-17 3:07 
QuestionHello Pin
Member 967019313-Apr-13 5:09
Member 967019313-Apr-13 5:09 
AnswerRe: VB Pin
Charles Putney13-Apr-13 6:57
Charles Putney13-Apr-13 6:57 
GeneralRe: VB Pin
Member 967019315-Apr-13 3:24
Member 967019315-Apr-13 3:24 
Questioncannt work well on Win7 Pin
Man in the mirror31-Oct-12 16:22
Man in the mirror31-Oct-12 16:22 
QuestionListview NF Pin
Haloniels199717-Oct-12 8:53
Haloniels199717-Oct-12 8:53 
GeneralMy vote of 1 Pin
i003-Jul-12 16:57
i003-Jul-12 16:57 
GeneralMy vote of 4 Pin
Burak Tunçbilek19-Jun-12 2:19
Burak Tunçbilek19-Jun-12 2:19 
GeneralChange netsh winXP by netsh Windows 7 o vista Pin
Member 76683229-Apr-11 1:22
Member 76683229-Apr-11 1:22 
GeneralRe: Change netsh winXP by netsh Windows 7 o vista Pin
Charles Putney9-Apr-11 10:28
Charles Putney9-Apr-11 10:28 
GeneralUrgence :WifiScanner for windows XP Pin
noureddine29-Mar-11 3:34
noureddine29-Mar-11 3:34 
GeneralRe: Urgence :WifiScanner for windows XP Pin
Charles Putney29-Mar-11 5:22
Charles Putney29-Mar-11 5:22 
GeneralRe: Urgence :WifiScanner for windows XP Pin
noureddine31-Mar-11 7:05
noureddine31-Mar-11 7:05 
GeneralMy vote of 5 Pin
radiomajak11-Jul-10 21:58
radiomajak11-Jul-10 21:58 
GeneralNetsh equivalente para Windows XP Pin
ManolinRamirez30-Mar-10 18:25
ManolinRamirez30-Mar-10 18:25 
GeneralRe: Netsh equivalente para Windows XP Pin
Charles Putney30-Mar-10 19:53
Charles Putney30-Mar-10 19:53 
GeneralA question about the signal Pin
zky8505215-May-09 22:51
zky8505215-May-09 22:51 
GeneralRe: A question about the signal Pin
Charles Putney6-May-09 0:04
Charles Putney6-May-09 0:04 
GeneralRe: A question about the signal Pin
zky8505216-May-09 17:29
zky8505216-May-09 17:29 
GeneralRe: A question about the signal Pin
notaclue129-Jul-09 11:05
notaclue129-Jul-09 11:05 
GeneralRe: A question about the signal Pin
Charles Putney15-Oct-10 0:20
Charles Putney15-Oct-10 0:20 
QuestionPossibility to Associate? Pin
sud_net27-Aug-08 4:03
sud_net27-Aug-08 4:03 
AnswerRe: Possibility to Associate? Pin
Charles Putney3-Sep-08 21:01
Charles Putney3-Sep-08 21:01 
QuestionWiFi in Java Pin
mulllllen17-Aug-08 22:50
mulllllen17-Aug-08 22:50 
AnswerRe: WiFi in Java Pin
Charles Putney20-Aug-08 6:11
Charles Putney20-Aug-08 6:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.