利用cmd命令创建wifi热点
2012-05-18 21:53
351 查看
核心代码:
/** * 利用cmd命令创建wifi热点 * */ private void btCreateWifi_Click(object sender, EventArgs e) { string hotSpotName = wifiName.Text.Trim(); string hotSpotPass = wifiPass.Text.Trim(); if (hotSpotPass.Length < 8) { MessageBox.Show("密码必须大于8位,请重新输入..."); wifiPass.Focus(); } else { if (createHotSpot(hotSpotName, hotSpotPass)) MessageBox.Show("wifi热点创建成功\n热点名为:" + hotSpotName + "\n密码为:" + hotSpotPass); else MessageBox.Show("wifi热点创建失败"); } } /** * 执行Cmd命令创建wifi热点 * */ public Boolean createHotSpot(string hotSpotName, string hotSpotPass) { //创建wifi热点三部曲,仅适用于win7下 string cmd1 = "netsh wlan set hostednetwork mode=allow"; string cmd2 = "netsh wlan set hostednetwork ssid=" + hotSpotName + " key=" + hotSpotPass; string cmd3 = "netsh wlan start hostednetwork"; string[] cmd = new string[] { cmd1, cmd2, cmd3 }; string rs = execMutipleCmd(cmd); cmdMsg.AppendText(rs); return regexCheckIfSuccess(rs, "已启动承载网络"); } /** * 用正则匹配是否成功 * */ public Boolean regexCheckIfSuccess(string msg, string reg) { Regex r = new Regex(reg); Match m = r.Match(msg); if (m.Success) return true; else return false; } private void stopWifi_Click(object sender, EventArgs e) { if (stopHotSpot()) MessageBox.Show("禁止wifi热点成功"); else MessageBox.Show("禁止操作失败"); } public Boolean stopHotSpot() { string cmd = "netsh wlan set hostednetwork mode=disallow"; string rs = execSingleCmd(cmd); cmdMsg.AppendText(rs); return regexCheckIfSuccess(rs, "承载网络模式已设置为禁止"); } /** * 执行单条命令 * */ public static string execSingleCmd(string cmd) { //因为cmd直接在window system32目录下,所以无需加路径 ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe"); //设置不显示cmd窗口 startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardOutput = true; Process process = Process.Start(startInfo); process.StandardInput.AutoFlush = true; process.StandardInput.WriteLine(cmd); process.StandardInput.WriteLine("exit"); //等待程序执行完退出进程 process.WaitForExit(); //截获输出流 string rs = process.StandardOutput.ReadToEnd(); process.Close(); return rs; } /** * 执行多条命令 * */ public static string execMutipleCmd(string[] cmd) { //因为cmd直接在window system32目录下,所以无需加路径 ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe"); //设置不显示cmd窗口 startInfo.CreateNoWindow = true; startInfo.UseShellExecute = false; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardOutput = true; Process process = Process.Start(startInfo); process.StandardInput.AutoFlush = true; for (int i = 0; i < cmd.Length; ++i) process.StandardInput.WriteLine(cmd[i]); process.StandardInput.WriteLine("exit"); //等待程序执行完退出进程 process.WaitForExit(); //截获输出流 string rs = process.StandardOutput.ReadToEnd(); process.Close(); return rs; }
相关文章推荐
- 利用cmd命令创建wifi热点
- 利用Mac创建一个 IPv6 WIFI 热点
- win10系统中创建wifi热点及被禁用后重新启用
- WIFI热点——bat文件创建笔记本快捷wifi技术
- [Ubuntu 14.04] 创建可以用于Android的WIFI热点
- 使用Ubuntu12.04创建无线WiFi热点供手机上网
- Win10系统下创建WiFi热点供手机或平板使用
- android wifi操作,热点创建
- 在windows7系统上如何利用命令提示符(cmd)创建一个既安全又稳定的wifi
- 笔记本创建WIFI热点
- 通过connectify在windows7下创建无线wifi热点(解决Y460失败方法)
- [转载]Windows 7笔记本创建wifi热点供手机上网教程
- wind7 下面组建共享宽带上网局域网 利用wifi热点
- win7笔记本创建WiFi热点
- ubuntu 14.04 利用ap-hotspot建立wifi热点失效
- 公交Wifi热点方案--利用Web认证页面实现广告投放
- wind7 下面组建共享宽带上网局域网 利用wifi热点
- win7创建wifi热点脚本分享
- MAC或iOS 创建 IPv6 WIFI热点
- Win8创建wifi热点