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

 更新时间:2013年04月14日 11:59:02   作者:  
最近想在win7上开启wifi热点,于是就弄出下面这个小东西,里面涉及如何在控制台上输入命令,分享一下。首先在VS中创建一个window窗口,然后创建两个四个button,两个输入框

要点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# 制作gif的四种方法

    c# 制作gif的四种方法

    这篇文章主要介绍了c# 制作gif的四种方法,帮助大家更好的理解和学习c#编程语言,感兴趣的朋友可以了解下
    2020-12-12
  • C#实现IP代理池调度的示例代码

    C#实现IP代理池调度的示例代码

    这篇文章主要为大家介绍了C#实现IP代理池调度的相关知识,文中的示例代码讲解详细,具有一定的参考与学习价值,感兴趣的小伙伴可以了解一下
    2023-07-07
  • 使用GetInvalidFileNameCharts生成文件名

    使用GetInvalidFileNameCharts生成文件名

    这篇文章主要介绍了一个很实用的函数Path.GetInvalidFileNameCharts(),他可以很方便的生成一个有效的文件名称
    2014-01-01
  • 浅谈C#下winform和JS的互相调用和传参(webbrowser)

    浅谈C#下winform和JS的互相调用和传参(webbrowser)

    下面小编就为大家带来一篇浅谈C#下winform和JS的互相调用和传参(webbrowser)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • C#实现坦克大战游戏

    C#实现坦克大战游戏

    这篇文章主要为大家详细介绍了C#实现坦克大战游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • Unity Shader实现玻璃材质效果

    Unity Shader实现玻璃材质效果

    这篇文章主要为大家详细介绍了Unity Shader实现玻璃材质效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • c#判断数据库服务器是否已经启动的方法

    c#判断数据库服务器是否已经启动的方法

    这篇文章主要介绍了使用c#判断数据库服务器是否已经启动的方法,大家参考使用吧
    2014-01-01
  • C#实现自定义圆角按钮的方法

    C#实现自定义圆角按钮的方法

    Winform中自带的button没有圆角属性,所以我们继承Button类,重写OnPaint事件来绘制圆角按钮。下面通过实例代码给大家介绍下C#实现自定义圆角按钮的方法,需要的朋友参考下吧
    2021-11-11
  • c# 连接池的设置与使用

    c# 连接池的设置与使用

    这篇文章主要介绍了c# 连接池的设置与使用,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下
    2021-01-01
  • c#与js随机数生成方法

    c#与js随机数生成方法

    这篇文章主要介绍了c#与js随机数生成方法,实例分析了C#与js生成随机数的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-02-02

最新评论