WiFi热点

1.string.Empty、""和null

    null:不引用任何对象的空引用文字值。

  “”:表示空字符串,理论上重新定义内存空间。

  string.Empty:表示空字符串,指向某一处内存空间。

  (来源:https://www.cnblogs.com/liuyaozhi/p/5809521.html)

2.判断字符串

string.IsNullOrEmpty:判断字符是否为空。例:string.IsNullOrEmpty(字符串)
Contains():判断是否包含字符
IndexOf():获取字符的位置

3.string.Format进行字符串拼接。

  这个和python类似,用来做字符串的拼接。

Form1.cs代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WIFI
{
    public partial class Form1 : Form
    {
        string account = string.Empty;  // 定义一个空字符串
        string pwd = string.Empty;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {

        }
        // 开始按钮
        private void button4_Click(object sender, EventArgs e)
        {
            account = this.textBox1.Text.Trim();  // Trim用来去除空格
            pwd = this.textBox2.Text.Trim();
            // string.IsNullOrEmpty判断是否为空
            if ((!string.IsNullOrEmpty(account)) && (pwd.Length > 8))
            {
                // 开启网卡: netsh wlan set hostednetwork mode=allow ssid={0} key={1}
                string cmd_str = string.Format("netsh wlan set hostednetwork mode=allow ssid={0} key={1}", account, pwd);
                string result = WIFI.Common.CmdHelp.Execute(cmd_str);
                if (result.Contains("承载网络模式已设置为允许"))
                {
                    string str = "netsh wlan start hostednetwork";
                    string str_start = WIFI.Common.CmdHelp.Execute(str);
                    if (str_start.Contains("已启动承载网络"))
                    {
                        MessageBox.Show("开启成功");
                    }else
                    {
                        MessageBox.Show("开启失败");
                    }
                }
                return;
            }
            MessageBox.Show("账号不为空且密码不能少于8位", "错误提示");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

CmdHelp.cs

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

namespace WIFI.Common
{
    public static class CmdHelp
    {
        public static string Execute(string dosCommand)
        {
            return Execute(dosCommand, 10);
        }
        public static string Execute(string command, int seconds)
        {
            string output = "";
            if (command != null && command.Equals(""))
            {
         // 这里不做详细介绍,进程当一个模块介绍 Process process
= new Process(); // 创建新进程 ProcessStartInfo startInfo = new ProcessStartInfo(); // 配置进程信息 startInfo.FileName = "cmd.exe"; // 设定需要执行的命令 startInfo.Arguments = "/C " + command; // “/C”表示执行完马上退出 startInfo.UseShellExecute = false; // 不使用系统外壳程序 startInfo.RedirectStandardInput = false; // 不重定向输入 startInfo.RedirectStandardOutput = true; // 重定向输出 startInfo.CreateNoWindow = true; // 不创建窗口 process.StartInfo = startInfo; try { if (process.Start()) { if(seconds == 0) { process.WaitForExit(); // 结束等待 }else { process.WaitForExit(seconds); // 等待结束 } output = process.StandardOutput.ReadToEnd(); // 读取进程的输出 } } catch { } finally { if (process != null) { process.Close(); } } } return output; } } }

猜你喜欢

转载自www.cnblogs.com/namejr/p/10332206.html