C#中调用命令行cmd开启wifi热点的实例代码
发布时间:2023-11-28 03:15:04
要点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";
}
}
}
}


猜你喜欢
- Android调试出现The selected device is incompatible问题解决在做Android调试时碰到该问题。详情
- spring.activemq.pool.enabled=false时,每发送一条数据都需要创建一个连接,这样会出现频繁创建和销毁连接的场景
- this总要有个事物来代表类的当前对象,就像C++中的this指针一样,Java中的this关键字就是代表当前对象的引用。它有三个主要的作用
- 一、概述之前公司app里面有个功能是一个可以双向滑动的范围选择器,我在网上百度过一些实现方法,感觉各有利弊吧,但是都不太适合我们的需求。所以
- 一.理论准备流是个抽象的概念,是对输入输出设备的抽象,Java程序中,对于数据的输入/输出操作都是以“流”的方式进行,设备可以是文件、网络、
- 本文实例为大家分享了java实现酒店管理系统的具体代码,供大家参考,具体内容如下要求:【酒店管理系统】HotelSystem.java某酒店
- 在使用spring提供的JpaTemplate进行查询时,如果数据量超过100 条,查询效率就会明显降低。由于开始时使用JPA内部的双向关联
- 网上关于下拉刷新的文章也不少,不过都太长了。恰好发现了官方的下拉刷新库,而且效果还是不错的,简洁美观,用得也挺方便。下面是效果图:我的好友原
- ArrayList就是传说中的动态数组,它提供了如下一些好处:动态的增加和减少元素实现了ICollection和IList接口灵活的设置数组
- hystrixDashboard服务监控除了隔离依赖服务的调用以外,Hystrix还提供了准实时的调用监控(Hystrix Dashboar
- 在C# winform应用程序中,用以下代码可以进行一些内存使用的优化using System;using System.Diagnosti
- Mybatis注解查找@Select( "SELECT * FROM tt_user WHERE username Like #{
- (1)很多朋友在使用genymotion开发安卓应用程序的时候,会遇见完全正确的安装但是在运行的时候仍然找不到,genymotion上的设备
- 什么是EJB?EJB 是 Java 企业Bean, 是JavaEE服务端 企业组件模型,它的设计目标与核心应用是部署分布式应用程序。话不多说
- 1. 前言什么是特殊矩阵?C++,一般使用二维数组存储矩阵数据。在实际存储时,会发现矩阵中有许多值相同的数据或有许多零数据,且分布呈现出一定
- 一、引言之前小编讲了MP从入门到核心功能的使用,接下来这几天小编会把MP在实际项目中,一些常用的高级功能给记录一下。高级功能分为:逻辑删除、
- 在一些音乐类应用中, 经常会展示随着节奏上下起伏的波纹信息, 这些波纹形象地传达了声音信息, 可以提升用户体验, 那么是如何实现的呢? 可以
- 本文实例讲述了Android实现手机壁纸改变的方法。分享给大家供大家参考。具体如下:main.xml布局文件:<?xml versio
- 关于Unity的换装,网上有几篇文章,我之前也简单的描述过实现。不过那个时候只是粗略的试验了下。今天好好梳理了下代码。先上代码(自己的游戏项
- 引言在Broker中,事务消息的初始化是通过BrokerController.initialTransaction()方法执行的。priva