■ 와이파이 스캐너 사용하기

----------------------------------------------------------------------------------------------------


TestWiFiScanner.zip


MainForm.cs

 

 

using System;

using System.Text;

using System.Threading;

using System.Windows.Forms;

 

using NativeWifi;

 

namespace TestWiFiScanner

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 리스트 뷰 지우기 대리자 - ClearListViewDelegate(listView)

 

        /// <summary>

        /// 리스트 뷰 지우기 대리자

        /// </summary>

        /// <param name="listView">리스트 뷰</param>

        private delegate void ClearListViewDelegate(ListView listView);

 

        #endregion

        #region 리스트 뷰 항목 추가하기 대리자 - AddListViewItemDelegate(listView, listViewItem)

 

        /// <summary>

        /// 리스트 뷰 항목 추가하기 대리자

        /// </summary>

        /// <param name="listView">리스트 뷰</param>

        /// <param name="listViewItem">리스트 뷰 항목</param>

        private delegate void AddListViewItemDelegate(ListView listView, ListViewItem listViewItem);

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 리스트 뷰 지우기 대리자

        /// </summary>

        private ClearListViewDelegate clearListViewDelegate = null;

 

        /// <summary>

        /// 리스트 뷰 항목 추가하기 대리자

        /// </summary>

        private AddListViewItemDelegate addListViewItemDelegate = null;

 

        /// <summary>

        /// WLAN 클라이언트

        /// </summary>

        private WlanClient wlanClient = new WlanClient();

 

        /// <summary>

        /// 스레드

        /// </summary>

        private Thread thread = null;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            Load        += form_Load;

            FormClosing += form_FormClosing;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 폼 로드시 처리하기 - form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void form_Load(object sender, EventArgs e)

        {

            this.clearListViewDelegate   = new ClearListViewDelegate(ClearListView);

            this.addListViewItemDelegate = new AddListViewItemDelegate(AddListViewItem);

 

            this.thread = new Thread(UpdateListView);

 

            this.thread.Start();

        }

 

        #endregion

        #region 폼을 닫을 경우 처리하기 - form_FormClosing(sender, e)

 

        /// <summary>

        /// 폼을 닫을 경우 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void form_FormClosing(object sender, FormClosingEventArgs e)

        {

            if(this.thread != null)

            {

                this.thread.Abort();

            }

 

            Application.ExitThread();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 리스트 뷰 지우기 - ClearListView(listView)

 

        /// <summary>

        /// 리스트 뷰 지우기

        /// </summary>

        /// <param name="listView">리스트 뷰</param>

        private void ClearListView(ListView listView)

        {

            listView.Items.Clear();

        }

 

        #endregion

        #region 리스트 뷰 항목 추가하기 - AddListViewItem(listView, listViewItem)

 

        /// <summary>

        /// 리스트 뷰 항목 추가하기

        /// </summary>

        /// <param name="listView">리스트 뷰</param>

        /// <param name="listViewItem">리스트 뷰 항목</param>

        private void AddListViewItem(ListView listView, ListViewItem listViewItem)

        {

            listView.Items.Add(listViewItem);

        }

 

        #endregion

        #region SSID 문자열 구하기 - GetSSIDString(wlanAvailableNetwork)

 

        /// <summary>

        /// SSID 문자열 구하기

        /// </summary>

        /// <param name="wlanAvailableNetwork">WLAN 이용 가능한 네트워크</param>

        /// <returns>SSID 문자열</returns>

        private string GetSSIDString(Wlan.WlanAvailableNetwork wlanAvailableNetwork)

        {

            Wlan.Dot11Ssid dot11Ssid = wlanAvailableNetwork.dot11Ssid;

 

            return Encoding.ASCII.GetString(dot11Ssid.SSID, 0, (int)dot11Ssid.SSIDLength);

        }

 

        #endregion

        #region 신호 강도 문자열 구하기 - GetSignalQualityString(wlanAvailableNetwork)

 

        /// <summary>

        /// 신호 강도 문자열 구하기

        /// </summary>

        /// <param name="wlanAvailableNetwork">WLAN 이용 가능한 네트워크</param>

        /// <returns>신호 강도 문자열</returns>

        private string GetSignalQualityString(Wlan.WlanAvailableNetwork wlanAvailableNetwork)

        {

            string signalQuality = wlanAvailableNetwork.wlanSignalQuality.ToString();

 

            return signalQuality;

        }

 

        #endregion

        #region 암호화 여부 문자열 구하기 - GetSecurityEnabledString(wlanAvailableNetwork)

 

        /// <summary>

        /// 암호화 여부 문자열 구하기

        /// </summary>

        /// <param name="wlanAvailableNetwork">WLAN 이용 가능한 네트워크</param>

        /// <returns>암호화 여부 문자열</returns>

        private string GetSecurityEnabledString(Wlan.WlanAvailableNetwork wlanAvailableNetwork)

        {

            bool securityEnabled = wlanAvailableNetwork.securityEnabled;

 

            return securityEnabled ? "암호화" : "-";

        }

 

        #endregion

        #region MAC 주소 문자열 구하기 - GetMACAddressString(ssidByteArray)

 

        /// <summary>

        /// MAC 주소 문자열 구하기

        /// </summary>

        /// <param name="ssidByteArray">SSID 바이트 배열</param>

        /// <returns>MAC 주소 문자열</returns>

        private string GetMACAddressString(byte[] ssidByteArray)

        {

            string macAddressString = string.Empty;

 

            for(int i = 0; i < 6; i++)

            {

                macAddressString += ssidByteArray[i].ToString("X2") + "-";

            }

 

            return macAddressString.Substring(0, macAddressString.Length - 1);

        }

 

        #endregion

        #region MAC 주소 문자열 구하기 - GetMACAddressString(wlanAvailableNetwork)

 

        /// <summary>

        /// MAC 주소 문자열 구하기

        /// </summary>

        /// <param name="wlanAvailableNetwork">WLAN 이용 가능한 네트워크</param>

        /// <returns>MAC 주소 문자열</returns>

        private string GetMACAddressString(Wlan.WlanAvailableNetwork wlanAvailableNetwork)

        {

            byte[] ssidByteArray = wlanAvailableNetwork.dot11Ssid.SSID;

 

            string macAddressString = GetMACAddressString(ssidByteArray);

 

            return macAddressString;

        }

 

        #endregion

        #region 채널 문자열 구하기 - GetChannelString(macAddressString)

 

        /// <summary>

        /// 채널 문자열 구하기

        /// </summary>

        /// <param name="macAddressString">MAC 주소 문자열</param>

        /// <returns>채널 문자열</returns>

        private string GetChannelString(string macAddressString)

        {

            Wlan.WlanBssEntry[] wlanBssEntryArray = this.wlanClient.Interfaces[0].GetNetworkBssList();

 

            foreach(Wlan.WlanBssEntry wlanBssEntry in wlanBssEntryArray)

            {

                if(GetMACAddressString(wlanBssEntry.dot11Ssid.SSID) == macAddressString)

                {

                    string centerFrequencyString = wlanBssEntry.chCenterFrequency.ToString();

 

                    switch(centerFrequencyString)

                    {

                        case "2412000" : return "1";

                        case "2417000" : return "2";

                        case "2422000" : return "3";

                        case "2427000" : return "4";

                        case "2432000" : return "5";

                        case "2437000" : return "6";

                        case "2442000" : return "7";

                        case "2447000" : return "8";

                        case "2452000" : return "9";

                        case "2457000" : return "10";

                        case "2462000" : return "11";

                        case "2467000" : return "12";

                        case "2472000" : return "13";

                    }

                }

            }

 

            return null;

        }

 

        #endregion

        #region 암호화 알고리즘 문자열 구하기 - GetCipherAlgorithmString(wlanAvailableNetwork)

 

        /// <summary>

        /// 암호화 알고리즘 문자열 구하기

        /// </summary>

        /// <param name="wlanAvailableNetwork">WLAN 이용 가능한 네트워크</param>

        /// <returns>암호화 알고리즘 문자열</returns>

        private string GetCipherAlgorithmString(Wlan.WlanAvailableNetwork wlanAvailableNetwork)

        {

            string cipherAlgorithmString = wlanAvailableNetwork.dot11DefaultCipherAlgorithm.ToString();

 

            return cipherAlgorithmString == "None" ? "-" : cipherAlgorithmString;

        }

 

        #endregion

        #region 인증 알고리즘 문자열 구하기 - GetAuthenticationAlgorithmString(wlanAvailableNetwork)

 

        /// <summary>

        /// 인증 알고리즘 문자열 구하기

        /// </summary>

        /// <param name="wlanAvailableNetwork">WLAN 이용 가능한 네트워크</param>

        /// <returns>인증 알고리즘 문자열</returns>

        private string GetAuthenticationAlgorithmString(Wlan.WlanAvailableNetwork wlanAvailableNetwork)

        {

            string authenticationAlgorithmString = wlanAvailableNetwork.dot11DefaultAuthAlgorithm.ToString();

 

            return authenticationAlgorithmString;

        }

 

        #endregion

        #region 리스트 뷰 갱신하기 - UpdateListView()

 

        /// <summary>

        /// 리스트 뷰 갱신하기

        /// </summary>

        private void UpdateListView()

        {

            while(true)

            {

                Invoke(this.clearListViewDelegate, this.listView);

 

                Wlan.WlanAvailableNetwork[] wlanAvailableNetworkArray = this.wlanClient.Interfaces[0].GetAvailableNetworkList(0);

 

                int count = wlanAvailableNetworkArray.Length;

 

                for(int i = 0; i < count; i++)

                {

                    Wlan.WlanAvailableNetwork wlanAvailableNetwork = wlanAvailableNetworkArray[i];

 

                    string ssidString                    = GetSSIDString(wlanAvailableNetwork);

                    string signalQualityString           = GetSignalQualityString(wlanAvailableNetwork);

                    string securityEnabledString         = GetSecurityEnabledString(wlanAvailableNetwork);

                    string macAddressString              = GetMACAddressString(wlanAvailableNetwork);

                    string channelString                 = GetChannelString(macAddressString);

                    string cipherAlgorithmString         = GetCipherAlgorithmString(wlanAvailableNetwork);

                    string authenticationAlgorithmString = GetAuthenticationAlgorithmString(wlanAvailableNetwork);

 

                    ListViewItem listViewItem = new ListViewItem

                    (

                        new string[]

                        {

                            ssidString,

                            signalQualityString,

                            securityEnabledString,

                            channelString,

                            cipherAlgorithmString,

                            authenticationAlgorithmString,

                            macAddressString

                        }

                    );

 

                    Invoke(this.addListViewItemDelegate, this.listView, listViewItem);

                }

 

                Thread.Sleep(10000);

            }

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요