首先,我們要寫一個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)
{
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].ToString() != "" && 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("密碼必須是八位數以上,十一位數以下");
}
}
}