C#程序如何實現設置系統WIFI共享


百度結果WIN7如何設置WIFI上網

1、以管理員身份運行命令提示符:
快捷鍵win+R→輸入cmd→回車
2、啟用並設定虛擬WiFi網卡:
運行命令:netsh wlan set hostednetwork mode=allow ssid=mywifi key=12345678
此命令有三個參數,mode:是否啟用虛擬WiFi網卡,改為disallow則為禁用。
                               ssid:無線網名稱,最好用英文(以mywifi為例)。
                               key:無線網密碼,八個以上字符(以12345678為例)。
以上三個參數可以單獨使用,例如只使用mode=disallow可以直接禁用虛擬Wifi網卡。
2,開啟成功后,網絡連接中會多出一個網卡為“Microsoft Virtual WiFi Miniport Adapter”的無線連接。若沒有,只需更新無線網卡驅動就OK了。
3、設置Internet連接共享:
在“網絡連接”窗口中,右鍵單擊已連接到Internet的網絡連接,選擇“屬性”→“共享”,勾上“允許其他······連接(N)”並選擇“虛擬WiFi”。
確定之后,提供共享的網卡圖標旁會出現“共享的”字樣,表示“寬帶連接”已共享至“虛擬WiFi”。
4、開啟無線網絡:
繼續在命令提示符中運行:netsh wlan start hostednetwork
(將start改為stop即可關閉該無線網,以后開機后要啟用該無線網只需再次運行此命令即可)
至此,虛擬WiFi的紅叉叉消失,WiFi基站已組建好。筆記本、帶WiFi模塊的手機等子機搜索到無線網絡mywifi,輸入密碼12345678,就能共享上網啦!

每次開啟電腦都需要這樣設置一次很麻煩,而且家里還有個電腦小白,所以想寫個軟件實現基本功能

本來按照上面思路已經可以開始動手寫代碼,結果百度一下已經有大能實現並找到源碼,

所以拿來主義自己改了改如圖所示

主要代碼

View Code
   //新建 
        private void btnCreate_Click(object sender, EventArgs e)
        {
            if ((textName.Text == "") || (textPsw.Text == ""))
            {
                ListBoxLogs.AddCtrlValue(this, sysLogs, DateTime.Now.ToString("HH:mm:ss") + "---" + "用戶名和密碼均不能為空!");
            }
            else if (textPsw.Text.Length < 8)
            {
                ListBoxLogs.AddCtrlValue(this, sysLogs, DateTime.Now.ToString("HH:mm:ss") + "---" + "密碼不能少於8位!");
            }
            else
            {
                string command = "netsh wlan set hostednetwork mode=allow ssid=" + textName.Text + " key=" + textPsw.Text;
                string str2 = executeCmd(command);
                if (((str2.IndexOf("承載網絡模式已設置為允許") > -1) && (str2.IndexOf("已成功更改承載網絡的 SSID。") > -1)) && (str2.IndexOf("已成功更改托管網絡的用戶密鑰密碼。") > -1))
                {
                    ListBoxLogs.AddCtrlValue(this, sysLogs, DateTime.Now.ToString("HH:mm:ss") + "---" + "新建共享網絡成功!");
                }
                else
                {
                    ListBoxLogs.AddCtrlValue(this, sysLogs, DateTime.Now.ToString("HH:mm:ss") + "---" + "搭建失敗,請重試!");
                }
            }
        }
        //刪除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            string command = "netsh wlan set hostednetwork mode=disallow";
            if (executeCmd(command).IndexOf("承載網絡模式已設置為禁止") > -1)
            {
                ListBoxLogs.AddCtrlValue(this, sysLogs, DateTime.Now.ToString("HH:mm:ss") + "---" + "禁止共享網絡成功!");
            }
            else
            {
                ListBoxLogs.AddCtrlValue(this, sysLogs, DateTime.Now.ToString("HH:mm:ss") + "---" + "操作失敗,請重試!");
            }
        }
        //啟動
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (executeCmd("netsh wlan start hostednetwork").IndexOf("已啟動承載網絡") > -1)
            {
                ListBoxLogs.AddCtrlValue(this, sysLogs, DateTime.Now.ToString("HH:mm:ss") + "---" + "已啟動承載網絡!");
            }
            else
            {
                ListBoxLogs.AddCtrlValue(this, sysLogs, DateTime.Now.ToString("HH:mm:ss") + "---" + "承載失敗,請嘗試新建網絡共享!");
            }
        }
        //停止
        private void btnStop_Click(object sender, EventArgs e)
        {
            if (executeCmd("netsh wlan stop hostednetwork").IndexOf("已停止承載網絡") > -1)
            {
                ListBoxLogs.AddCtrlValue(this, sysLogs, DateTime.Now.ToString("HH:mm:ss") + "---" + "已停止承載網絡!");
            }
            else
            {
                ListBoxLogs.AddCtrlValue(this, sysLogs, DateTime.Now.ToString("HH:mm:ss") + "---" + "停止承載失敗!");
            }
        }
View Code
public class ListBoxLogs
    {
        private delegate void AddCtrlValueHandler(Control ctrl, string value);
        private delegate void ChangeComboBoxValueHandler(ComboBox ctrl);
        private delegate void SetCtrlEnableHandler(Control ctrl, bool value);
        private delegate void SetCtrlValueHandler(Control ctrl, string value);

        public static void AddCtrlValue(Form parentForm, Control ctrl, string value)
        {
            if (parentForm.InvokeRequired)
            {
                AddCtrlValueHandler method = new AddCtrlValueHandler(AddCtrlValueMethod);
                parentForm.BeginInvoke(method, new object[] { ctrl, value });
            }
            else
            {
                AddCtrlValueMethod(ctrl, value);
            }
        }

        private static void AddCtrlValueMethod(Control ctrl, string value)
        {
            if (ctrl is TextBox)
            {
                TextBox box = ctrl as TextBox;
                box.Text = box.Text + value;
            }
           else if (ctrl is Label)
            {
                Label label = ctrl as Label;
                label.Text = label.Text + value;
            }
            else if (ctrl is ListBox)
            {
                ListBox listbox = ctrl as ListBox;
                if (listbox.Items.Count > 200)
                {
                    listbox.Items.Clear();
                }
                listbox.Items.Add(value);
                if (listbox.Items.Count > 1)
                {
                    listbox.SelectedIndex = (listbox.Items.Count - 1);
                }
            }
            else if (ctrl is RichTextBox)
            {
                RichTextBox richtextbox = ctrl as RichTextBox;
                richtextbox.Text += value + "\r\n";
                if (richtextbox.Text.Length > 6000)
                {
                    richtextbox.Text = string.Empty;
                }
            }

        }

        public static void ChangeComboBoxValue(Form parentForm, ComboBox ctrl)
        {
            if (parentForm.InvokeRequired)
            {
                ChangeComboBoxValueHandler method = new ChangeComboBoxValueHandler(ChangeComboBoxValueMethod);
                parentForm.BeginInvoke(method, new object[] { ctrl });
            }
            else
            {
                ChangeComboBoxValueMethod(ctrl);
            }
        }

        private static void ChangeComboBoxValueMethod(ComboBox ctrl)
        {
            if (ctrl.Items.Count > 1)
            {
                if (ctrl.SelectedIndex == 0)
                {
                    ctrl.SelectedIndex = 1;
                }
                else
                {
                    ctrl.SelectedIndex = 0;
                }
            }
        }

        public static void SetCtrlEnable(Form parentForm, Control ctrl, bool value)
        {
            if (parentForm.InvokeRequired)
            {
                SetCtrlEnableHandler method = new SetCtrlEnableHandler(SetCtrlEnableMethod);
                parentForm.BeginInvoke(method, new object[] { ctrl, value });
            }
            else
            {
                SetCtrlEnableMethod(ctrl, value);
            }
        }

        public static void SetCtrlEnable(UserControl parentCtrl, Control ctrl, bool value)
        {
            if (parentCtrl.InvokeRequired)
            {
                SetCtrlEnableHandler method = new SetCtrlEnableHandler(SetCtrlEnableMethod);
                parentCtrl.BeginInvoke(method, new object[] { ctrl, value });
            }
            else
            {
                SetCtrlEnableMethod(ctrl, value);
            }
        }

        private static void SetCtrlEnableMethod(Control ctrl, bool value)
        {
            //if (ctrl is TextBox)
            //{
            //    TextBox box = ctrl as TextBox;
            //    box.Enabled = value;
            //}
            //if (ctrl is ComboBox)
            //{
            //    ComboBox box2 = ctrl as ComboBox;
            //    box2.Enabled = value;
            //}
            //if (ctrl is Label)
            //{
            //    Label label = ctrl as Label;
            //    label.Enabled = value;
            //}
            //if (ctrl is Button)
            //{
            //    Button button = ctrl as Button;
            //    button.Enabled = value;
            //}
            //if (ctrl is NumericUpDown)
            //{
            //    NumericUpDown down = ctrl as NumericUpDown;
            //    down.Enabled = value;
            //}
            //if (ctrl is Form)
            //{
            //    Form form = ctrl as Form;
            //    form.Enabled = value;
            //}
            ////if (ctrl is IPTextBox)
            ////{
            ////    IPTextBox box3 = ctrl as IPTextBox;
            ////    box3.Enabled = value;
            ////}
            //if (ctrl is GroupBox)
            //{
            //    GroupBox box4 = ctrl as GroupBox;
            //    box4.Enabled = value;
            //}
            //if (ctrl is CheckBox)
            //{
            //    CheckBox box5 = ctrl as CheckBox;
            //    box5.Enabled = value;
            //}
            try
            {
                ctrl.Enabled = value;
            }
            catch { }
        }

        public static void SetCtrlValue(Form parentForm, Control ctrl, string value)
        {
            if (parentForm.InvokeRequired)
            {
                SetCtrlValueHandler method = new SetCtrlValueHandler(SetCtrlValueMethod);
                parentForm.BeginInvoke(method, new object[] { ctrl, value });
            }
            else
            {
                SetCtrlValueMethod(ctrl, value);
            }
        }

        public static void SetCtrlValue(UserControl parentCtrl, Control ctrl, string value)
        {
            if (parentCtrl.InvokeRequired)
            {
                SetCtrlValueHandler method = new SetCtrlValueHandler(SetCtrlValueMethod);
                parentCtrl.BeginInvoke(method, new object[] { ctrl, value });
            }
            else
            {
                SetCtrlValueMethod(ctrl, value);
            }
        }

        private static void SetCtrlValueMethod(Control ctrl, string value)
        {
            if (ctrl is TextBox)
            {
                TextBox box = ctrl as TextBox;
                box.Text = value;
            }
            else if (ctrl is ComboBox)
            {
                ComboBox box2 = ctrl as ComboBox;
                try
                {
                    int selIndex = 0;
                    try
                    {
                        selIndex = int.Parse(value);
                        if (selIndex < box2.Items.Count - 1)
                        {
                            box2.SelectedIndex = selIndex;
                        }
                        else
                        {
                            box2.SelectedIndex = box2.FindString(value);
                        }
                    }
                    catch
                    {
                        box2.SelectedIndex = box2.FindString(value);
                    }

                }
                catch (Exception exception)
                {
                    //LogFile.Log.Debug(exception.Message);
                }
            }
            else if (ctrl is Label)
            {
                Label label = ctrl as Label;
                label.Text = value;
            }
            else if (ctrl is Button)
            {
                Button button = ctrl as Button;
                button.Text = value;
            }
            else if (ctrl is NumericUpDown)
            {
                NumericUpDown down = ctrl as NumericUpDown;
                down.Value = int.Parse(value);
            }
            else if (ctrl is Form)
            {
                Form form = ctrl as Form;
                form.Text = value;
            }
            else if (ctrl is ProgressBar)
            {
                ProgressBar bar = ctrl as ProgressBar;
                bar.Value = int.Parse(value);
            }
            else if (ctrl is CheckBox)
            {
                try
                {
                    CheckBox cb = ctrl as CheckBox;
                    cb.Checked = bool.Parse(value);
                }
                catch
                {
                }
            }
            else
            {
                ctrl.Text = value;
            }
        }

        private delegate void SetCtrlVisibleHandler(Control ctrl, bool value);
        public static void SetCtrlVisible(Form parentForm, Control ctrl, bool value)
        {
            if (parentForm.InvokeRequired)
            {
                SetCtrlVisibleHandler method = new SetCtrlVisibleHandler(SetCtrlVisibleMethod);
                parentForm.BeginInvoke(method, new object[] { ctrl, value });
            }
            else
            {
                SetCtrlVisibleMethod(ctrl, value);
            }
        }

        private static void SetCtrlVisibleMethod(Control ctrl, bool value)
        {
            try
            {
                ctrl.Visible = value;
            }
            catch { }
        }

        private delegate void SetCtrlTagHandler(Control ctrl, string value);
        public static void SetCtrlTag(Form parentForm, Control ctrl, string value)
        {
            if (parentForm.InvokeRequired)
            {
                SetCtrlTagHandler method = new SetCtrlTagHandler(SetCtrlTagMethod);
                parentForm.BeginInvoke(method, new object[] { ctrl, value });
            }
            else
            {
                SetCtrlTagMethod(ctrl, value);
            }
        }

        private static void SetCtrlTagMethod(Control ctrl, string value)
        {
            try
            {
                ctrl.Tag = value;
            }
            catch { }
        }
    }

功能完成,測試成功(必須有無限網卡)
代碼下載連接http://download.csdn.net/detail/huangyuancao/5096582

歡迎各位吐槽。。。。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM