C#中调用命令行cmd开启wifi热点的实例代码
要点1:cmd命令行的输入命令
netshwlansethostednetworkmode=allowssid=用户名 key=密码
netshwlanstarthostednetwork
netshwalnstophostednetwork
netshinterfaceipsetaddressname="本地连接"source=dhcp
要点2:在C#中调用cmd.exe命令行
privatevoidcreate(stringstr)
{
//process用于调用外部程序
System.Diagnostics.Processp=newSystem.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");
}
详细的代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespacewifi01
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
}
//“创建wifi热点”按钮
privatevoidbutton1_Click(objectsender,EventArgse)
{
stringstr;
stringuserName=textBox1.Text;
stringpassword=textBox2.Text;
if(password.Length>=8&&userName!=null)
{
//命令行输入命令,用来新建wifi
str="netshwlansethostednetworkmode=allowssid="+userName+"key="+password;
create(str);
MessageBox.Show("新建了wifi热点",
"新建成功",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
label4.Text="新建了wifi热点";
}
else
{
MessageBox.Show("你的账号为空或你的密码长度小于8",
"登陆失败",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}
//"开启wifi"按钮
privatevoidbutton2_Click(objectsender,EventArgse)
{
//命令行输入命令,
stringstr="netshwlanstarthostednetwork";
create(str);
label4.Text="已启动wifi热点";
}
//“关闭wifi”按钮
privatevoidbutton3_Click(objectsender,EventArgse)
{
//命令行输入命令,
stringstr="netshwlanstophostednetwork";
create(str);
label4.Text="已关闭wifi热点";
}
//在cmd控制台输入命令,
privatevoidcreate(stringstr)
{
//process用于调用外部程序
System.Diagnostics.Processp=newSystem.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连接按钮
privatevoidbutton4_Click(objectsender,EventArgse)
{
//命令行输入命令,用来自动连接wifi:netshinterfaceipsetaddressname="本地连接"source=dhcp
stringstr="netshinterfaceipsetaddressname=\"本地连接\"source=dhcp";
stringstr1="锐捷是否提示你设置自动获取IP\n"+"或你想自动获取IP,请按确定";
DialogResultresult=MessageBox.Show(str1,"自动连接IP",
MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
if(result==DialogResult.OK)
{
create(str);
label4.Text="锐捷自动获取IP";
}
}
}
}
相关文章
- C#MQTTNET客户端使用说明
- c# mysql executenonquery_C#与数据库访问技术总结(八)之ExecuteNonQuery方法
- C# 笔记之基本语法
- C#利用SharpZipLib生成压缩包
- 分享一款漂亮的 C# .Net 图形验证码
- 【地铁上的Redis与C#】数据类型(十三)--综合案例
- C# 滑动拼图验证码
- Linux给你带来的随身WiFi体验(linux随身wifi)
- Linux下破解WiFi新技术(linux破解wifi)
- 使用Linux命令连接WiFi(linux命令连接wifi)
- Linux横扫无线世界:WiFi全面发展的新纪元(linux的wifi)
- C程序操作MySQL数据库的锁表实现方法(c# mysql 锁表)
- C#小数位数保留的方法集锦
- jQuery获取地址栏参数插件(模仿C#)
- C#短时间内产生大量不重复的随机数
- c#深拷贝与浅拷贝的区别分析及实例
- C#中读写INI文件的方法例子
- c#构造ColorComboBox(颜色下拉框)
- Asp.Net(C#)自动执行计划任务的程序实例分析分享
- C#实现鼠标移动到曲线图上显示值的方法
- C#实现获取文本文件的编码的一个类(区分GB2312和UTF8)