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

所属分类: 软件编程 / C#教程 阅读数: 48
收藏 0 赞 0 分享

要点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";
            }

        }
    }
}

更多精彩内容其他人还在看

C#中Datetimepicker出现问题的解决方法

这篇文章主要给大家介绍了关于C#中Datetimepicker出现问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C# SQLite数据库入门使用说明

这篇文章主要给大家介绍了关于C#中SQLite数据库入门使用的相关资料,文中通过图文以及示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#实现批量下载图片到本地示例代码

这篇文章主要给大家介绍了关于C#如何实现批量下载图片到本地的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用c#具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

如何获取C#中方法的执行时间以及其代码注入详解

这篇文章主要给大家介绍了关于如何获取C#中方法的执行时间以及其代码注入的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧
收藏 0 赞 0 分享

C#中通过LRU实现通用高效的超时连接探测

这篇文章主要介绍了c#中通过LRU实现通用高效的超时连接探测,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

这篇文章主要给大家介绍了关于如何使用C#将Tensorflow训练的.pb文件用在生产环境的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#程序启动项的设置方法

这篇文章主要为大家详细介绍了C#程序启动项的设置方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

c#爬虫爬取京东的商品信息

这篇文章主要给大家介绍了关于利用c#爬虫爬取京东商品信息的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#随机数生成字母金字塔

这篇文章主要为大家详细介绍了C#随机数生成字母金字塔,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

WPF实现窗体中的悬浮按钮

这篇文章主要为大家详细介绍了WPF实现窗体中的悬浮按钮,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多