C#中调用命令行cmd开启wifi热点的实例代码

时间:2023-11-28 03:15:04 

C#中调用命令行cmd开启wifi热点的实例代码

要点1:cmd命令行的输入命令
netsh wlan set hostednetwork mode=allow ssid=用户名  key=密码
netsh wlan start hostednetwork
netsh waln stop hostednetwork
netsh interface ip set address name="本地连接" source=dhcp


要点2:在C#中调用cmd.exe命令行


       private void create(string str)
        {
            //process用于调用外部程序
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            //调用cmd.exe
            p.StartInfo.FileName = "cmd.exe";
            //是否指定操作系统外壳进程启动程序
            p.StartInfo.UseShellExecute = false;
            //可能接受来自调用程序的输入信息
            //重定向标准输入
            p.StartInfo.RedirectStandardInput = true;
            //重定向标准输出
            p.StartInfo.RedirectStandardOutput = true;
            //重定向错误输出
            p.StartInfo.RedirectStandardError = true;
            //不显示程序窗口
            p.StartInfo.CreateNoWindow = true;
            //启动程序
            p.Start();
            //睡眠1s。
            System.Threading.Thread.Sleep(1000);
            //输入命令
            p.StandardInput.WriteLine(str);
            //一定要关闭。
            p.StandardInput.WriteLine("exit");
        }


详细的代码如下:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace wifi01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
           //“创建wifi热点”按钮
        private void button1_Click(object sender, EventArgs e)
        {
            string str;
            string userName = textBox1.Text;
            string password = textBox2.Text;
            if (password.Length >= 8 && userName != null)
            {
                    // 命令行输入命令,用来新建wifi
                str="netsh wlan set hostednetwork mode=allow ssid="+userName+" key="+password;
                create(str);
                MessageBox.Show("新建了wifi热点",
                    "新建成功",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                label4.Text = "新建了wifi热点";
            }
            else
            {
                MessageBox.Show("你的账号为空或你的密码长度小于8",
                    "登陆失败",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);
            }
        }
           //"开启wifi"按钮
        private void button2_Click(object sender, EventArgs e)
        {
                // 命令行输入命令,
            string str = "netsh wlan start hostednetwork";
            create(str);
            label4.Text = "已启动wifi热点";
        }
          //“关闭wifi”按钮
        private void button3_Click(object sender, EventArgs e)
        {
                // 命令行输入命令,
            string str = "netsh wlan stop hostednetwork";
            create(str);
            label4.Text = "已关闭wifi热点";
        }
           //在cmd控制台输入命令,
        private void create(string str)
        {
            //process用于调用外部程序
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            //调用cmd.exe
            p.StartInfo.FileName = "cmd.exe";
            //是否指定操作系统外壳进程启动程序
            p.StartInfo.UseShellExecute = false;
            //可能接受来自调用程序的输入信息
            //重定向标准输入
            p.StartInfo.RedirectStandardInput = true;
            //重定向标准输出
            p.StartInfo.RedirectStandardOutput = true;
            //重定向错误输出
            p.StartInfo.RedirectStandardError = true;
            //不显示程序窗口
            p.StartInfo.CreateNoWindow = true;
            //启动程序
            p.Start();
            //睡眠1s。
            System.Threading.Thread.Sleep(1000);
            //输入命令
            p.StandardInput.WriteLine(str);
            //一定要关闭。
            p.StandardInput.WriteLine("exit");
        }
           //自动IP连接 按钮
        private void button4_Click(object sender, EventArgs e)
        {
               // 命令行输入命令,用来自动连接wifi:netsh interface ip set address name="本地连接" source=dhcp
            string str="netsh interface ip set address name=\"本地连接\" source=dhcp";
            string str1 = "锐捷是否提示你设置自动获取IP\n"+"或你想自动获取IP,请按确定";
            DialogResult result = MessageBox.Show(str1,"自动连接IP",
                MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
            if (result == DialogResult.OK)
            {
                create(str);
                label4.Text = "锐捷自动获取IP";
            }

        }
    }
}


标签:cmd,wifi,热点
0
投稿

猜你喜欢

  • C#巧用DateTime预设可选的日期范围(如本年度、本季度、本月等)

    2022-09-19 11:06:41
  • Android应用开发之将SQLite和APK一起打包的方法

    2023-07-03 04:16:07
  • 字符串阵列String[]转换为整型阵列Int[]的实例

    2021-07-22 08:19:17
  • Android自定义有限制区域图例角度自识别涂鸦工具类中篇

    2021-06-16 16:21:46
  • SpringBoot集成SpringSecurity和JWT做登陆鉴权的实现

    2023-01-29 09:34:57
  • MybatisPlus字段类型转换的实现示例

    2022-12-09 22:29:13
  • java反射使用示例分享

    2023-07-02 20:18:59
  • Android编程之Sdcard相关代码集锦

    2022-08-15 09:55:24
  • winform实现可拖动的自定义Label控件

    2022-12-14 09:11:36
  • Android Rreact Native 常见错误总结

    2021-07-11 16:39:59
  • Java Spring Security认证与授权及注销和权限控制篇综合解析

    2021-07-04 08:14:17
  • Java结构型设计模式之享元模式示例详解

    2022-05-16 21:41:10
  • Java中的字节流文件读取教程(一)

    2023-08-14 08:14:47
  • 如何从UA分辨出Android设备类型

    2023-09-03 00:37:44
  • Android中去掉标题栏的几种方法(三种)

    2023-04-01 10:57:37
  • mybaties plus selectMaps和selectList的区别说明

    2021-07-15 11:18:29
  • Java基于外观模式实现美食天下食谱功能实例详解

    2022-08-22 22:59:51
  • Android实现换肤的两种思路分析

    2023-03-25 13:54:18
  • java取两个字符串的最大交集

    2021-07-30 17:23:08
  • SpringBoot统一响应格式及统一异常处理

    2022-08-30 08:03:08
  • asp之家 软件编程 m.aspxhome.com