C#程序如何实现设置系统WIFI共享-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

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,就能共享上网啦!

每次开启电脑都需要这样设置一次很麻烦,而且家里还有个电脑小白,所以想写个软件实现基本功能

本来按照上面思路已经可以开始动手写代码,结果百度一下已经有大能实现并找到源码,

所以拿来主义自己改了改如图所示

主要代码

复制代码
   //新建 
        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") + "---" + "停止承载失败!");
            }
        }
复制代码
复制代码
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

欢迎各位吐槽。。。。

 本文转自夜&枫博客园博客,原文链接:http://www.cnblogs.com/newstart/archive/2013/02/28/2937303.html,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章