• 利用cmd命令创建wifi热点


    核心代码:

      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[i]);
    116             process.StandardInput.WriteLine("exit");
    117             //等待程序执行完退出进程
    118             process.WaitForExit();
    119             //截获输出流
    120             string rs = process.StandardOutput.ReadToEnd();
    121             process.Close();
    122             return rs;
    123         }
  • 相关阅读:
    004 cat、head、tail、vim、cp、mv、rm
    003 系统的结构目录、pwd、cd、ls、tree、mkdir、touch
    shell编程
    多线程
    接口(适配器)
    常用方法
    爬虫要具备的准则:
    知识点扫盲篇
    记录_20190628
    记录_20190626
  • 原文地址:https://www.cnblogs.com/trying/p/2863744.html
Copyright © 2020-2023  润新知