您的位置:首页 > 编程语言 > C#

用C#来调用cmd命令程序,实现wifi控制的工具

2015-10-16 10:26 701 查看
首先,我们要写一个cmd的命令调用方法,网上有很多,自己整理了一个,写进了一个类,这样用的方便点,直接新建一个类复制进去就行了

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;

namespace DesktopShortcut
{
class Chuanzhilei
{
private string cmbtext;

public string Cmbtext
{
get { return cmbtext; }
set { cmbtext = value; }
}
/// <summary>
/// 执行cmd命令的方法,测试可用
/// </summary>
/// <param name="commendtxt">cmd命令代码</param>
/// <returns></returns>
public string exeCommand(string commendtxt)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;

string stroutput = null;
try
{
p.Start();
p.StandardInput.WriteLine(commendtxt);
p.StandardInput.WriteLine("exit");
stroutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch (Exception e)
{
stroutput = e.Message;
}

return stroutput;
}
}
}

窗体就简单很多,只用一个下拉列表来控制,功能有:

1)创建wifi

2)查看wifi

3)修改wifi

4)开启wifi

5)关闭wifi

6)删除wifi

7)wifi连接状态

主要就是各种判断限制有点烦,要判断wifi连接的状态是如何的,有没有创建wifi,关闭程序的时候wifi确不确定是关闭的等等,源码我发到下面,有兴趣的哥们可以复制粘贴一起交流一下...

主窗体代码:所用控件有,一个按钮,一个下拉列表

public partial class Form1 : Form
{
/// <summary>
/// 全局变量
/// </summary>
public string WifiName = "Pan";//默认wifi名称
public string WifiProsswork = "03345678";//默认wifi密码
Chuanzhilei cmdtext = new Chuanzhilei();//C#执行cmd命令方法的实例化
public int i = 0;

public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (i == 0)
{
comboBox1.Text = "请选择功能";
if (!File.Exists("D:\\wifi信息.txt"))//如果D盘目录下没有wifi信息这个txt文件,就创建,有的话就继续判断
{

FileStream fs1 = new FileStream("D:\\wifi信息.txt", FileMode.Create, FileAccess.Write);
//在D盘根目录下创建txt文档并写入文件
StreamWriter sw = new StreamWriter(fs1);
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("");
sw.Close();
fs1.Close();
MessageBox.Show("欢迎使用");
}
else//如果有的话判断
{
string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);
//判断"wifi信息"这个文档内保存的wifi信息有没有符合条件
if (line[2] == "" && line[0].ToString() == "" && line[1].ToString() == "")
{
MessageBox.Show("未曾创建过wifi,请先创建wifi");
comboBox1.SelectedIndex = 0;
}
else if (line[0].ToString() == "" && line[1].ToString() == "")
{
if (line[2].ToString() != "")
{
MessageBox.Show("曾经创建过wifi,使用功能直接开启wifi即可");

4000
comboBox1.SelectedIndex = 3;
}
else
{
MessageBox.Show("wifi是开启的状态");
}
}
}
}
else
{
comboBox1.Text = "请选择功能";
}
}
private void button2_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex < 0)
{
MessageBox.Show("请选择功能");
comboBox1.Text = "请选择功能";
}
else
{
if (comboBox1.SelectedIndex == 0)//创建wifi
{
string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);
if (line[2].ToString() == "")
{
if (line[0].ToString() == "" && line[1].ToString().Length < 2 )
{
Wifi wifi = new Wifi();
wifi.wifi = comboBox1.SelectedIndex;
wifi.Show();
this.Visible = false;
}
}
else
{
if (line[2].ToString() == "true")
{
MessageBox.Show("wifi已创建并是开启状态!");
}
else
{
MessageBox.Show("wifi已创建,开启即可");
comboBox1.SelectedIndex = 3;
}

}

}
else if (comboBox1.SelectedIndex == 1)//查看wifi
{

try
{
string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);
//string[] line;
//line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);
if (line[0].ToString().Length > 1 && line[1].ToString().Length > 1)
{
this.TopMost = true;
MessageBox.Show(line[0].ToString() + "\n" + line[1].ToString());
}
else
{
this.TopMost = true;
MessageBox.Show("wifi关闭或并未创建wifi");
comboBox1.SelectedIndex = 0;
}

}
catch
{
this.TopMost = true;
MessageBox.Show("未创建wifi热点");
}
}
else if (comboBox1.SelectedIndex == 2)//修改wifi
{
string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);
if (line[0].ToString() != "" && line[1].ToString() != "")
{
Wifi wifi = new Wifi();
wifi.wifi = comboBox1.SelectedIndex;
wifi.Show();
this.Visible = false;
}
else
{
this.TopMost = true;
MessageBox.Show("未创建wifi热点");
}
}
else if (comboBox1.SelectedIndex == 3)//开启wifi
{
string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);
//string[] line;
//line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);
if (line[0].ToString() == "" && line[1].ToString() == "")
{
line[0] = "wifiName:" + WifiName + "";
line[1] = "wifiPasswork:" + WifiProsswork + "";
line[2] = "true";
File.WriteAllLines(@"D:\\wifi信息.txt", line);
cmdtext.exeCommand("netsh wlan start hostednetwork");
this.TopMost = true;
MessageBox.Show("wifi已开启" + "\n" + "wifi名为:" + WifiName + "" + "\n" + "密码为:" + WifiProsswork + "");
}
else if (line[0].ToString() != "" && line[1].ToString().Length > 2)
{
line[2] = "true";
File.WriteAllLines(@"D:\\wifi信息.txt", line);
this.TopMost = true;
MessageBox.Show("wifi已开启,请勿重复操作");
}

}
else if (comboBox1.SelectedIndex == 4)//关闭wifi
{
string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);
if (line[0].ToString() != "" && line[1].ToString().Length > 1)
{
if (line[2].ToString() == "false")
{
MessageBox.Show("wifi已关闭!不要重复操作");
}
else
{
cmdtext.exeCommand("netsh wlan stop hostednetwork");
this.TopMost = true;
line[0] = "";
line[1] = "";
line[2] = "false";
File.WriteAllLines(@"D:\\wifi信息.txt", line);
MessageBox.Show("wifi已关闭!");
}

}
else
{
this.TopMost = true;
MessageBox.Show("wifi并未被创建");
}
}
else if (comboBox1.SelectedIndex == 5)//删除wifi热点
{
string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);
if (line[0].ToString() != "" && line[1].ToString().Length > 2)
{
line[0] = "";
line[1] = "";
line[2] = "";
File.WriteAllLines(@"D:\\wifi信息.txt", line);
cmdtext.exeCommand("netsh wlan stop hostednetwork");
this.TopMost = true;
MessageBox.Show("删除成功!");
}
else
{
MessageBox.Show("wifi并未被创建");
}
}
else if (comboBox1.SelectedIndex == 6)//查看wifi
{
string str = cmdtext.exeCommand("netsh wlan show hostednetwork");
this.TopMost = true;
MessageBox.Show(str);
}
else if (comboBox1.SelectedIndex == 7)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.Start();
}
else if (comboBox1.SelectedIndex == 8)//定时关机
{
SleepTimer queding = new SleepTimer();
queding.str = this.comboBox1.SelectedIndex;
queding.Show();
this.Hide();
}
else if (comboBox1.SelectedIndex == 9)//取消关机
{
cmdtext.exeCommand("shutdown -a");
MessageBox.Show("命令已执行!");
this.TopMost = true;
}
else if (comboBox1.SelectedIndex == 10)//打开任务管理器
{
cmdtext.exeCommand("taskmgr");

}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)//窗口正在关闭的时候执行的事件
{
string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);
if (MessageBox.Show("是否退出", "退出", MessageBoxButtons.OKCancel) == DialogResult.OK)//判断是不是选择"确定",是执行下面代码
{
if (line[2].ToString() == "true")
{
if (MessageBox.Show("是否退出,但不关闭wifi", "退出", MessageBoxButtons.OKCancel) == DialogResult.OK)//判断是不是选择"确定",是执行下面代码
{
line[0] = "";
line[1] = "";
line[2] = "false";
File.WriteAllLines(@"D:\\wifi信息.txt", line);
MessageBox.Show("谢谢使用,wifi已保留");
this.Dispose();
Application.Exit();
}
else
{
if (MessageBox.Show("是否关闭wifi,并退出", "退出", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
cmdtext.exeCommand("netsh wlan stop hostednetwork");
line[0] = "";
line[1] = "";
line[2] = "false";
File.WriteAllLines(@"D:\\wifi信息.txt", line);
MessageBox.Show("谢谢使用");
this.Dispose();
Application.Exit();
}
else
{
e.Cancel = true;
}

}
}
else
{
MessageBox.Show("谢谢使用");
this.Dispose();
Application.Exit();
}

}
else
{
e.Cancel = true;
}
}


创建wifi,修改wifi弹出的窗体:

所用控件,两个textBox,两个按钮

public partial class Wifi : Form
{

public int wifi;
Form1 form1 = new Form1();//实例化主窗体
Chuanzhilei cmdtext = new Chuanzhilei();
public Wifi()
{

InitializeComponent();
}

private void Wifi_Load(object sender, EventArgs e)
{
form1.i += 1;
//获取一个txt文档的数据,保存在一个string类型的数组内
string[] line;
line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);
if (wifi == 0)
{
button1.Visible = true;//创建wifi按钮
button2.Visible = false;//修改wifi按钮
textBox1.Text = form1.WifiName;
textBox2.Text = form1.WifiProsswork;
}
else
{
if (line[0].ToString().Length > 1 && line[1].ToString().Length > 1)
{
MessageBox.Show("请输入修改信息");
string wifiname = line[0].ToString();//获取txt文档内的第一行,wifi名称
string wifipasswork = line[1].ToString();//获取txt文档内的第二行,wifi密码
textBox1.Text = wifiname.Substring(wifiname.IndexOf(':') + 1);
textBox2.Text = wifipasswork.Substring(wifipasswork.IndexOf(':') + 1);
button1.Visible = false;
button2.Visible = true;
}
else
{
MessageBox.Show("wifi并未被创建");
this.Close();
}
}

}
private void Wifi_FormClosed(object sender, FormClosedEventArgs e)
{
form1.Visible = true;
form1.TopMost = true;
}

private void button1_Click(object sender, EventArgs e)//创建wifi按钮
{
//判断创建的密码是不是八位数以上十一位数以下,控制密码位数
if (textBox2.Text.Length >= 8 && textBox2.Text.Length <= 11)
{
//获取一个txt文档的数据,保存在一个string类型的数组内
string[] line;
line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);
try
{
if (line[0].ToString() == textBox1.Text && line[1].ToString() == textBox1.Text)
{
MessageBox.Show("wifi已创建,不许重复操作");
this.Close();
}
else
{
//执行创建wifi的cmd命令
string exetext = "netsh wlan set hostednetwork mode=allow ssid=" + textBox1.Text + " key=" + textBox2.Text;
cmdtext.exeCommand(exetext);
cmdtext.exeCommand("netsh wlan start hostednetwork");
//将创建好的wifi名称和wifi密码保存到主窗体用以判断
form1.WifiName = textBox1.Text;
form1.WifiProsswork = textBox2.Text;
//将创建好的wifi名称和wifi密码保存到一个txt文档内
line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);
line[0] = "wifiName:" + textBox1.Text + "";
line[1] = "wifiPasswork:" + textBox2.Text + "";
line[2] = "true";
File.WriteAllLines(@"D:\\wifi信息.txt", line);

MessageBox.Show("创建成功, \n wifi名为:" + textBox1.Text + " \n 密码为:" + textBox2.Text);
this.Close();
}
}
catch
{
string exetext = "netsh wlan set hostednetwork mode=allow ssid='" + textBox1.Text + "' key='" + textBox2.Text + "'";
cmdtext.exeCommand("netsh wlan stop hostednetwork");
cmdtext.exeCommand(exetext);
cmdtext.exeCommand("netsh wlan start hostednetwork");
StreamWriter sw = new StreamWriter("D:\\wifi信息.txt", true, Encoding.ASCII);
sw.WriteLine("wifiName:" + textBox1.Text + "");
sw.WriteLine("wifiPasswork:" + textBox2.Text + "");
sw.WriteLine("true");
sw.Flush();
sw.Close();
MessageBox.Show("默认wifi为:" + textBox1.Text + " \n 密码:" + textBox2.Text + "");
}

}
else
{
MessageBox.Show("密码必须是八位数以上,十一位数以下");
}
//}
}

private void button2_Click(object sender, EventArgs e)//修改wifi按钮
{
//将wifi帐号密码保存到主窗体用以判断已创建wifi
form1.WifiName = textBox1.Text;
form1.WifiProsswork = textBox2.Text;
if (textBox2.Text.Length >= 8 && textBox2.Text.Length <= 11)
{
//获取一个txt文档的数据,保存在一个string类型的数组内
string[] line;
line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);

//判断如果保存wifi信息的txt文档有数据,则执行修改,如果没有数据则报错
try
{
if (line[0].ToSt
90ae
ring() != "" && line[1].ToString().Length > 2)
{
//将修改的wifi信息保存到txt文档内
line[0] = "wifiName:" + textBox1.Text + "";
line[1] = "wifiPasswork:" + textBox2.Text + "";
line[2] = "true";
File.WriteAllLines(@"D:\\wifi信息.txt", line);

//开始执行创建wifi的cmd命令
cmdtext.exeCommand("netsh wlan stop hostednetwork");
string exetext = "netsh wlan set hostednetwork mode=allow ssid=" + textBox1.Text + " key=" + textBox2.Text;
cmdtext.exeCommand(exetext);
cmdtext.exeCommand("netsh wlan start hostednetwork");
//修改成功后提示
MessageBox.Show("修改成功, \n wifi名为:" + textBox1.Text + " \n 密码为:" + textBox2.Text);
}
}
catch
{
MessageBox.Show("并没有创建wifi");
}

}
else
{
MessageBox.Show("密码必须是八位数以上,十一位数以下");
}
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: