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

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

代码:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Diagnostics;  
  7. using System.IO;  
  8.   
  9. namespace DesktopShortcut  
  10. {  
  11.     class Chuanzhilei  
  12.     {  
  13.         private string cmbtext;  
  14.   
  15.         public string Cmbtext  
  16.         {  
  17.             get { return cmbtext; }  
  18.             set { cmbtext = value; }  
  19.         }  
  20.         /// <summary>  
  21.         /// 执行cmd命令的方法,测试可用  
  22.         /// </summary>  
  23.         /// <param name="commendtxt">cmd命令代码</param>  
  24.         /// <returns></returns>  
  25.         public string exeCommand(string commendtxt)  
  26.         {  
  27.             Process p = new Process();  
  28.             p.StartInfo.FileName = "cmd.exe";  
  29.             p.StartInfo.UseShellExecute = false;  
  30.             p.StartInfo.RedirectStandardInput = true;  
  31.             p.StartInfo.RedirectStandardOutput = true;  
  32.             p.StartInfo.RedirectStandardError = true;  
  33.   
  34.             string stroutput = null;  
  35.             try  
  36.             {  
  37.                 p.Start();  
  38.                 p.StandardInput.WriteLine(commendtxt);  
  39.                 p.StandardInput.WriteLine("exit");  
  40.                 stroutput = p.StandardOutput.ReadToEnd();  
  41.                 p.WaitForExit();  
  42.                 p.Close();  
  43.             }  
  44.             catch (Exception e)  
  45.             {  
  46.                 stroutput = e.Message;  
  47.             }  
  48.   
  49.             return stroutput;  
  50.         }  
  51.     }  
  52. }  
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确不确定是关闭的等等,源码我发到下面,有兴趣的哥们可以复制粘贴一起交流一下...
主窗体代码:所用控件有,一个按钮,一个下拉列表

  1. public partial class Form1 : Form  
  2.     {  
  3.         /// <summary>  
  4.         /// 全局变量  
  5.         /// </summary>  
  6.         public string WifiName = "Pan";//默认wifi名称  
  7.         public string WifiProsswork = "03345678";//默认wifi密码  
  8.         Chuanzhilei cmdtext = new Chuanzhilei();//C#执行cmd命令方法的实例化  
  9.         public int i = 0;  
  10.   
  11.         public Form1()  
  12.         {  
  13.             InitializeComponent();  
  14.         }  
  15.         private void Form1_Load(object sender, EventArgs e)  
  16.         {  
  17.             if (i == 0)  
  18.             {  
  19.                 comboBox1.Text = "请选择功能";  
  20.                 if (!File.Exists("D:\\wifi信息.txt"))//如果D盘目录下没有wifi信息这个txt文件,就创建,有的话就继续判断  
  21.                 {  
  22.   
  23.                     FileStream fs1 = new FileStream("D:\\wifi信息.txt", FileMode.Create, FileAccess.Write);  
  24.                     //在D盘根目录下创建txt文档并写入文件                   
  25.                     StreamWriter sw = new StreamWriter(fs1);  
  26.                     sw.WriteLine("");  
  27.                     sw.WriteLine("");  
  28.                     sw.WriteLine("");  
  29.                     sw.Close();  
  30.                     fs1.Close();  
  31.                     MessageBox.Show("欢迎使用");  
  32.                 }  
  33.                 else//如果有的话判断  
  34.                 {  
  35.                     string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);  
  36.                     //判断"wifi信息"这个文档内保存的wifi信息有没有符合条件  
  37.                     if (line[2] == "" && line[0].ToString() == "" && line[1].ToString() == "")  
  38.                     {  
  39.                         MessageBox.Show("未曾创建过wifi,请先创建wifi");  
  40.                         comboBox1.SelectedIndex = 0;  
  41.                     }  
  42.                     else if (line[0].ToString() == "" && line[1].ToString() == "")  
  43.                     {  
  44.                         if (line[2].ToString() != "")  
  45.                         {  
  46.                             MessageBox.Show("曾经创建过wifi,使用功能直接开启wifi即可");  
  47.                             comboBox1.SelectedIndex = 3;  
  48.                         }  
  49.                         else  
  50.                         {  
  51.                             MessageBox.Show("wifi是开启的状态");  
  52.                         }  
  53.                     }  
  54.                 }  
  55.             }  
  56.             else  
  57.             {  
  58.                 comboBox1.Text = "请选择功能";  
  59.             }  
  60.         }  
  61.         private void button2_Click(object sender, EventArgs e)  
  62.         {  
  63.             if (comboBox1.SelectedIndex < 0)  
  64.             {  
  65.                 MessageBox.Show("请选择功能");  
  66.                 comboBox1.Text = "请选择功能";  
  67.             }  
  68.             else  
  69.             {  
  70.                 if (comboBox1.SelectedIndex == 0)//创建wifi  
  71.                 {                    
  72.                     string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);  
  73.                     if (line[2].ToString() == "")  
  74.                     {  
  75.                         if (line[0].ToString() == "" && line[1].ToString().Length < 2 )  
  76.                         {  
  77.                             Wifi wifi = new Wifi();  
  78.                             wifi.wifi = comboBox1.SelectedIndex;  
  79.                             wifi.Show();  
  80.                             this.Visible = false;  
  81.                         }  
  82.                     }  
  83.                     else  
  84.                     {  
  85.                         if (line[2].ToString() == "true")  
  86.                         {  
  87.                             MessageBox.Show("wifi已创建并是开启状态!");  
  88.                         }  
  89.                         else  
  90.                         {  
  91.                             MessageBox.Show("wifi已创建,开启即可");  
  92.                             comboBox1.SelectedIndex = 3;  
  93.                         }  
  94.   
  95.                     }  
  96.   
  97.                 }  
  98.                 else if (comboBox1.SelectedIndex == 1)//查看wifi  
  99.                 {  
  100.   
  101.                     try  
  102.                     {  
  103.                         string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);  
  104.                         //string[] line;  
  105.                         //line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);  
  106.                         if (line[0].ToString().Length > 1 && line[1].ToString().Length > 1)  
  107.                         {  
  108.                             this.TopMost = true;  
  109.                             MessageBox.Show(line[0].ToString() + "\n" + line[1].ToString());  
  110.                         }  
  111.                         else  
  112.                         {  
  113.                             this.TopMost = true;  
  114.                             MessageBox.Show("wifi关闭或并未创建wifi");  
  115.                             comboBox1.SelectedIndex = 0;  
  116.                         }  
  117.   
  118.                     }  
  119.                     catch  
  120.                     {  
  121.                         this.TopMost = true;  
  122.                         MessageBox.Show("未创建wifi热点");  
  123.                     }  
  124.                 }  
  125.                 else if (comboBox1.SelectedIndex == 2)//修改wifi  
  126.                 {  
  127.                     string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);  
  128.                     if (line[0].ToString() != "" && line[1].ToString() != "")  
  129.                     {  
  130.                         Wifi wifi = new Wifi();  
  131.                         wifi.wifi = comboBox1.SelectedIndex;  
  132.                         wifi.Show();  
  133.                         this.Visible = false;  
  134.                     }  
  135.                     else  
  136.                     {  
  137.                         this.TopMost = true;  
  138.                         MessageBox.Show("未创建wifi热点");  
  139.                     }  
  140.                 }  
  141.                 else if (comboBox1.SelectedIndex == 3)//开启wifi  
  142.                 {  
  143.                     string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);  
  144.                     //string[] line;  
  145.                     //line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);  
  146.                     if (line[0].ToString() == "" && line[1].ToString() == "")  
  147.                     {  
  148.                         line[0] = "wifiName:" + WifiName + "";  
  149.                         line[1] = "wifiPasswork:" + WifiProsswork + "";  
  150.                         line[2] = "true";  
  151.                         File.WriteAllLines(@"D:\\wifi信息.txt", line);  
  152.                         cmdtext.exeCommand("netsh wlan start hostednetwork");  
  153.                         this.TopMost = true;  
  154.                         MessageBox.Show("wifi已开启" + "\n" + "wifi名为:" + WifiName + "" + "\n" + "密码为:" + WifiProsswork + "");  
  155.                     }  
  156.                     else if (line[0].ToString() != "" && line[1].ToString().Length > 2)  
  157.                     {  
  158.                         line[2] = "true";  
  159.                         File.WriteAllLines(@"D:\\wifi信息.txt", line);  
  160.                         this.TopMost = true;  
  161.                         MessageBox.Show("wifi已开启,请勿重复操作");  
  162.                     }  
  163.   
  164.   
  165.                 }  
  166.                 else if (comboBox1.SelectedIndex == 4)//关闭wifi  
  167.                 {  
  168.                     string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);  
  169.                     if (line[0].ToString() != "" && line[1].ToString().Length > 1)  
  170.                     {  
  171.                         if (line[2].ToString() == "false")  
  172.                         {  
  173.                             MessageBox.Show("wifi已关闭!不要重复操作");  
  174.                         }  
  175.                         else  
  176.                         {  
  177.                             cmdtext.exeCommand("netsh wlan stop hostednetwork");  
  178.                             this.TopMost = true;  
  179.                             line[0] = "";  
  180.                             line[1] = "";  
  181.                             line[2] = "false";  
  182.                             File.WriteAllLines(@"D:\\wifi信息.txt", line);  
  183.                             MessageBox.Show("wifi已关闭!");  
  184.                         }  
  185.   
  186.                     }  
  187.                     else  
  188.                     {  
  189.                         this.TopMost = true;  
  190.                         MessageBox.Show("wifi并未被创建");  
  191.                     }  
  192.                 }  
  193.                 else if (comboBox1.SelectedIndex == 5)//删除wifi热点  
  194.                 {  
  195.                     string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);  
  196.                     if (line[0].ToString() != "" && line[1].ToString().Length > 2)  
  197.                     {  
  198.                         line[0] = "";  
  199.                         line[1] = "";  
  200.                         line[2] = "";  
  201.                         File.WriteAllLines(@"D:\\wifi信息.txt", line);  
  202.                         cmdtext.exeCommand("netsh wlan stop hostednetwork");  
  203.                         this.TopMost = true;  
  204.                         MessageBox.Show("删除成功!");  
  205.                     }  
  206.                     else  
  207.                     {  
  208.                         MessageBox.Show("wifi并未被创建");  
  209.                     }  
  210.                 }  
  211.                 else if (comboBox1.SelectedIndex == 6)//查看wifi  
  212.                 {  
  213.                     string str = cmdtext.exeCommand("netsh wlan show hostednetwork");  
  214.                     this.TopMost = true;  
  215.                     MessageBox.Show(str);  
  216.                 }  
  217.                 else if (comboBox1.SelectedIndex == 7)  
  218.                 {  
  219.                     Process p = new Process();  
  220.                     p.StartInfo.FileName = "cmd.exe";  
  221.                     p.Start();  
  222.                 }  
  223.                 else if (comboBox1.SelectedIndex == 8)//定时关机  
  224.                 {  
  225.                     SleepTimer queding = new SleepTimer();  
  226.                     queding.str = this.comboBox1.SelectedIndex;  
  227.                     queding.Show();  
  228.                     this.Hide();  
  229.                 }  
  230.                 else if (comboBox1.SelectedIndex == 9)//取消关机  
  231.                 {  
  232.                     cmdtext.exeCommand("shutdown -a");  
  233.                     MessageBox.Show("命令已执行!");  
  234.                     this.TopMost = true;  
  235.                 }  
  236.                 else if (comboBox1.SelectedIndex == 10)//打开任务管理器  
  237.                 {  
  238.                     cmdtext.exeCommand("taskmgr");  
  239.   
  240.                 }  
  241.             }  
  242.         }  
  243.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)//窗口正在关闭的时候执行的事件  
  244.         {  
  245.             string[] line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);  
  246.             if (MessageBox.Show("是否退出""退出", MessageBoxButtons.OKCancel) == DialogResult.OK)//判断是不是选择"确定",是执行下面代码  
  247.             {  
  248.                 if (line[2].ToString() == "true")  
  249.                 {  
  250.                     if (MessageBox.Show("是否退出,但不关闭wifi""退出", MessageBoxButtons.OKCancel) == DialogResult.OK)//判断是不是选择"确定",是执行下面代码  
  251.                     {  
  252.                         line[0] = "";  
  253.                         line[1] = "";  
  254.                         line[2] = "false";  
  255.                         File.WriteAllLines(@"D:\\wifi信息.txt", line);  
  256.                         MessageBox.Show("谢谢使用,wifi已保留");  
  257.                         this.Dispose();  
  258.                         Application.Exit();  
  259.                     }  
  260.                     else  
  261.                     {  
  262.                         if (MessageBox.Show("是否关闭wifi,并退出""退出", MessageBoxButtons.OKCancel) == DialogResult.OK)  
  263.                         {  
  264.                             cmdtext.exeCommand("netsh wlan stop hostednetwork");  
  265.                             line[0] = "";  
  266.                             line[1] = "";  
  267.                             line[2] = "false";  
  268.                             File.WriteAllLines(@"D:\\wifi信息.txt", line);  
  269.                             MessageBox.Show("谢谢使用");  
  270.                             this.Dispose();  
  271.                             Application.Exit();  
  272.                         }  
  273.                         else  
  274.                         {  
  275.                             e.Cancel = true;  
  276.                         }  
  277.   
  278.                     }  
  279.                 }  
  280.                 else  
  281.                 {  
  282.                     MessageBox.Show("谢谢使用");  
  283.                     this.Dispose();  
  284.                     Application.Exit();  
  285.                 }  
  286.   
  287.             }  
  288.             else  
  289.             {  
  290.                 e.Cancel = true;  
  291.             }  
  292.         }  
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,两个按钮

  1. public partial class Wifi : Form  
  2.     {  
  3.   
  4.         public int wifi;  
  5.         Form1 form1 = new Form1();//实例化主窗体  
  6.         Chuanzhilei cmdtext = new Chuanzhilei();  
  7.         public Wifi()  
  8.         {  
  9.   
  10.             InitializeComponent();  
  11.         }  
  12.   
  13.         private void Wifi_Load(object sender, EventArgs e)  
  14.         {  
  15.             form1.i += 1;  
  16.             //获取一个txt文档的数据,保存在一个string类型的数组内  
  17.             string[] line;  
  18.             line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);  
  19.             if (wifi == 0)  
  20.             {  
  21.                 button1.Visible = true;//创建wifi按钮  
  22.                 button2.Visible = false;//修改wifi按钮  
  23.                 textBox1.Text = form1.WifiName;  
  24.                 textBox2.Text = form1.WifiProsswork;  
  25.             }  
  26.             else  
  27.             {  
  28.                 if (line[0].ToString().Length > 1 && line[1].ToString().Length > 1)  
  29.                 {  
  30.                     MessageBox.Show("请输入修改信息");  
  31.                     string wifiname = line[0].ToString();//获取txt文档内的第一行,wifi名称  
  32.                     string wifipasswork = line[1].ToString();//获取txt文档内的第二行,wifi密码  
  33.                     textBox1.Text = wifiname.Substring(wifiname.IndexOf(':') + 1);  
  34.                     textBox2.Text = wifipasswork.Substring(wifipasswork.IndexOf(':') + 1);  
  35.                     button1.Visible = false;  
  36.                     button2.Visible = true;  
  37.                 }  
  38.                 else  
  39.                 {  
  40.                     MessageBox.Show("wifi并未被创建");  
  41.                     this.Close();  
  42.                 }  
  43.             }  
  44.   
  45.   
  46.         }  
  47.         private void Wifi_FormClosed(object sender, FormClosedEventArgs e)  
  48.         {  
  49.             form1.Visible = true;  
  50.             form1.TopMost = true;  
  51.         }  
  52.   
  53.         private void button1_Click(object sender, EventArgs e)//创建wifi按钮  
  54.         {  
  55.             //判断创建的密码是不是八位数以上十一位数以下,控制密码位数  
  56.             if (textBox2.Text.Length >= 8 && textBox2.Text.Length <= 11)  
  57.             {  
  58.                 //获取一个txt文档的数据,保存在一个string类型的数组内  
  59.                 string[] line;  
  60.                 line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);  
  61.                 try  
  62.                 {  
  63.                     if (line[0].ToString() == textBox1.Text && line[1].ToString() == textBox1.Text)  
  64.                     {  
  65.                         MessageBox.Show("wifi已创建,不许重复操作");  
  66.                         this.Close();  
  67.                     }  
  68.                     else  
  69.                     {  
  70.                         //执行创建wifi的cmd命令  
  71.                         string exetext = "netsh wlan set hostednetwork mode=allow ssid=" + textBox1.Text + " key=" + textBox2.Text;  
  72.                         cmdtext.exeCommand(exetext);  
  73.                         cmdtext.exeCommand("netsh wlan start hostednetwork");  
  74.                         //将创建好的wifi名称和wifi密码保存到主窗体用以判断  
  75.                         form1.WifiName = textBox1.Text;  
  76.                         form1.WifiProsswork = textBox2.Text;  
  77.                         //将创建好的wifi名称和wifi密码保存到一个txt文档内  
  78.                         line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);  
  79.                         line[0] = "wifiName:" + textBox1.Text + "";  
  80.                         line[1] = "wifiPasswork:" + textBox2.Text + "";  
  81.                         line[2] = "true";  
  82.                         File.WriteAllLines(@"D:\\wifi信息.txt", line);  
  83.   
  84.                         MessageBox.Show("创建成功, \n wifi名为:" + textBox1.Text + " \n 密码为:" + textBox2.Text);  
  85.                         this.Close();  
  86.                     }  
  87.                 }  
  88.                 catch  
  89.                 {  
  90.                     string exetext = "netsh wlan set hostednetwork mode=allow ssid='" + textBox1.Text + "' key='" + textBox2.Text + "'";  
  91.                     cmdtext.exeCommand("netsh wlan stop hostednetwork");  
  92.                     cmdtext.exeCommand(exetext);  
  93.                     cmdtext.exeCommand("netsh wlan start hostednetwork");  
  94.                     StreamWriter sw = new StreamWriter("D:\\wifi信息.txt"true, Encoding.ASCII);  
  95.                     sw.WriteLine("wifiName:" + textBox1.Text + "");  
  96.                     sw.WriteLine("wifiPasswork:" + textBox2.Text + "");  
  97.                     sw.WriteLine("true");  
  98.                     sw.Flush();  
  99.                     sw.Close();  
  100.                     MessageBox.Show("默认wifi为:" + textBox1.Text + " \n 密码:" + textBox2.Text + "");  
  101.                 }  
  102.   
  103.             }  
  104.             else  
  105.             {  
  106.                 MessageBox.Show("密码必须是八位数以上,十一位数以下");  
  107.             }  
  108.             //}            
  109.         }  
  110.   
  111.         private void button2_Click(object sender, EventArgs e)//修改wifi按钮  
  112.         {  
  113.             //将wifi帐号密码保存到主窗体用以判断已创建wifi  
  114.             form1.WifiName = textBox1.Text;  
  115.             form1.WifiProsswork = textBox2.Text;  
  116.             if (textBox2.Text.Length >= 8 && textBox2.Text.Length <= 11)  
  117.             {  
  118.                 //获取一个txt文档的数据,保存在一个string类型的数组内  
  119.                 string[] line;  
  120.                 line = File.ReadAllLines(@"D:\\wifi信息.txt", System.Text.Encoding.Default);  
  121.   
  122.                 //判断如果保存wifi信息的txt文档有数据,则执行修改,如果没有数据则报错  
  123.                 try  
  124.                 {  
  125.                     if (line[0].ToString() != "" && line[1].ToString().Length > 2)  
  126.                     {  
  127.                         //将修改的wifi信息保存到txt文档内  
  128.                         line[0] = "wifiName:" + textBox1.Text + "";  
  129.                         line[1] = "wifiPasswork:" + textBox2.Text + "";  
  130.                         line[2] = "true";  
  131.                         File.WriteAllLines(@"D:\\wifi信息.txt", line);  
  132.   
  133.   
  134.                         //开始执行创建wifi的cmd命令  
  135.                         cmdtext.exeCommand("netsh wlan stop hostednetwork");  
  136.                         string exetext = "netsh wlan set hostednetwork mode=allow ssid=" + textBox1.Text + " key=" + textBox2.Text;  
  137.                         cmdtext.exeCommand(exetext);  
  138.                         cmdtext.exeCommand("netsh wlan start hostednetwork");  
  139.                         //修改成功后提示  
  140.                         MessageBox.Show("修改成功, \n wifi名为:" + textBox1.Text + " \n 密码为:" + textBox2.Text);  
  141.                     }  
  142.                 }  
  143.                 catch  
  144.                 {  
  145.                     MessageBox.Show("并没有创建wifi");  
  146.                 }  
  147.   
  148.   
  149.             }  
  150.             else  
  151.             {  
  152.                 MessageBox.Show("密码必须是八位数以上,十一位数以下");  
  153.             }  
  154.         }  
  155.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值