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

本文介绍了如何使用C#编程语言通过调用CMD命令来实现WiFi的创建、查看、修改、开启、关闭和删除等功能。文中提供了主窗体及创建/修改WiFi窗体的代码示例,强调了在操作中对WiFi状态判断的重要性。
摘要由CSDN通过智能技术生成

首先,我们要写一个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)
        
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值