设为首页 收藏本站
查看: 1069|回复: 0

[经验分享] 利用cmd命令创建wifi热点

[复制链接]

尚未签到

发表于 2015-9-30 13:56:40 | 显示全部楼层 |阅读模式
  核心代码:



  1 /**
  2          * 利用cmd命令创建wifi热点
  3          * */
  4         private void btCreateWifi_Click(object sender, EventArgs e)
  5         {
  6             string hotSpotName = wifiName.Text.Trim();
  7             string hotSpotPass = wifiPass.Text.Trim();
  8             if (hotSpotPass.Length < 8)
  9             {
10                 MessageBox.Show("密码必须大于8位,请重新输入...");
11                 wifiPass.Focus();
12             }
13             else
14             {
15                 if (createHotSpot(hotSpotName, hotSpotPass))
16                     MessageBox.Show("wifi热点创建成功\n热点名为:" + hotSpotName + "\n密码为:" + hotSpotPass);
17                 else
18                     MessageBox.Show("wifi热点创建失败");
19             }
20         }
21
22         /**
23          * 执行Cmd命令创建wifi热点
24          * */
25         public Boolean createHotSpot(string hotSpotName, string hotSpotPass)
26         {
27             //创建wifi热点三部曲,仅适用于win7下
28             string cmd1 = "netsh wlan set hostednetwork mode=allow";
29             string cmd2 = "netsh wlan set hostednetwork ssid=" + hotSpotName + " key=" + hotSpotPass;
30             string cmd3 = "netsh wlan start hostednetwork";
31
32             string[] cmd = new string[] { cmd1, cmd2, cmd3 };
33
34             string rs = execMutipleCmd(cmd);
35
36             cmdMsg.AppendText(rs);
37
38             return regexCheckIfSuccess(rs, "已启动承载网络");
39         }
40         /**
41          * 用正则匹配是否成功
42          * */
43         public Boolean regexCheckIfSuccess(string msg, string reg)
44         {
45             Regex r = new Regex(reg);
46             Match m = r.Match(msg);
47             if (m.Success)
48                 return true;
49             else
50                 return false;
51         }
52
53         private void stopWifi_Click(object sender, EventArgs e)
54         {
55             if (stopHotSpot())
56                 MessageBox.Show("禁止wifi热点成功");
57             else
58                 MessageBox.Show("禁止操作失败");
59         }
60
61         public Boolean stopHotSpot()
62         {
63             string cmd = "netsh wlan set hostednetwork mode=disallow";
64
65             string rs = execSingleCmd(cmd);
66             cmdMsg.AppendText(rs);
67             return regexCheckIfSuccess(rs, "承载网络模式已设置为禁止");
68         }
69         /**
70          * 执行单条命令
71          * */
72         public static string execSingleCmd(string cmd)
73         {
74             //因为cmd直接在window system32目录下,所以无需加路径
75             ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
76
77             //设置不显示cmd窗口
78             startInfo.CreateNoWindow = true;
79             startInfo.UseShellExecute = false;
80
81             startInfo.RedirectStandardInput = true;
82             startInfo.RedirectStandardOutput = true;
83
84             Process process = Process.Start(startInfo);
85             process.StandardInput.AutoFlush = true;
86
87             process.StandardInput.WriteLine(cmd);
88             process.StandardInput.WriteLine("exit");
89             //等待程序执行完退出进程
90             process.WaitForExit();
91             //截获输出流
92             string rs = process.StandardOutput.ReadToEnd();
93             process.Close();
94             return rs;
95         }
96
97         /**
98          * 执行多条命令
99          * */
100         public static string execMutipleCmd(string[] cmd)
101         {
102             //因为cmd直接在window system32目录下,所以无需加路径
103             ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
104
105             //设置不显示cmd窗口
106             startInfo.CreateNoWindow = true;
107             startInfo.UseShellExecute = false;
108
109             startInfo.RedirectStandardInput = true;
110             startInfo.RedirectStandardOutput = true;
111
112             Process process = Process.Start(startInfo);
113             process.StandardInput.AutoFlush = true;
114             for (int i = 0; i < cmd.Length; ++i)
115                 process.StandardInput.WriteLine(cmd);
116             process.StandardInput.WriteLine("exit");
117             //等待程序执行完退出进程
118             process.WaitForExit();
119             //截获输出流
120             string rs = process.StandardOutput.ReadToEnd();
121             process.Close();
122             return rs;
123         }
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-121007-1-1.html 上篇帖子: 打开关闭wifi服务-wifimanager 下篇帖子: WIFI模块 RTL8188EUS Realtek
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表