解决方案名称 BMWIFI
项目名称叫做BMWifi.Win
这个项目不需要使用数据库,只需要一个common的类库就行了
创建一个类库BMWifi.Conmmon
win项目 引用 common项目
--------------------------------------------------华丽的分割线----------------------------------------------------------
设置winform窗体的大小 标题 开始位置 图标 输出框架 按钮等等
通过窗体的属性来设置大小
修改窗体的名字
修改窗体标题名字
背景属性 backcolor
背景图片属性
鼠标在软件上的样式 cursor
修改图标 Icon 图标格式为icon
不允许设置为最大化
添加状态栏 和工具栏
打开工具箱 找到 statusStrip
添加工具箱 toolStrip
添加一个button,只显示文字
窗体的FormBorderstyle属性设置为:
禁用窗体的最大化,固定窗体的大小
添加label 和textbox
最终的效果:
-------------------------------------------------------------------------------------------------------------------------
cmd命令的执行
创建一个CmdHelper.cs用来操作 cmd命令
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace BMWifi.Common
{
public static class CmdHelper
{
public static string Execute(string dosCommand)
{
return Execute(dosCommand, 10);
}
/// <summary>
/// 执行DOS命令,返回DOS命令的输出
/// </summary>
/// <param name="command">dos命令</param>
/// <param name="seconds">等待命令执行的时间(单位:毫秒)
/// 设置为0,则无限等待</param>
/// <returns>返回DOS命令的输出</returns>
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;
}
}
}
主程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Wifi
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
string account = null;
string pwd = null;
/// <summary>
/// 开始 结束 点击按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
}
private void button1Start_MouseClick(object sender, MouseEventArgs e)
{
account = this.textBox1Accout.Text.Trim();//获得账号
pwd = this.textBox2Password.Text.Trim();
if (string.IsNullOrEmpty(account))//判断account这个变量是否为Null 或者空
{
MessageBox.Show("账号为空!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (pwd.Length < 8)
{
MessageBox.Show("密码少于8位!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//验证成功 执行cmd命令
//开启 网卡 netsh wlan set hostednetwork mode = allow ssid={0} key={1}
//拼接出我们的执行命令
string cmdstr = string.Format("netsh wlan set hostednetwork mode = allow ssid={0} key={1}", account, pwd);
//开启网卡
string res = BMWifi.Common.CmdHelper.Execute(cmdstr);
if (res.Contains("承载网络模式已设置为允许"))
{
MessageBox.Show("承载网络模式已设置为允许");
string StartCmd = "netsh wlan start hostednetwork";
string resStart = BMWifi.Common.CmdHelper.Execute(StartCmd);
if (resStart.Contains("已启动承载网络"))
{
MessageBox.Show("已启动承载网络");
}
else
{
MessageBox.Show("抱歉!开启失败,可以手动开启.");
}
}
}
}
}