首先,我们要写一个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即可");
comboBox1.SelectedIndex = 3;
}
else
{
MessageBox.Show("wifi是开启的状态");
}
}
}
}
else
{
comboBox1.Text = "请选择功能";
}
}
private void button2_Click(object sender, EventArgs e)