您的位置:首页 > 其它

利用cmd命令创建wifi热点

2012-05-18 21:53 99 查看
核心代码:

/**
* 利用cmd命令创建wifi热点
* */
private void btCreateWifi_Click(object sender, EventArgs e)
{
string hotSpotName = wifiName.Text.Trim();
string hotSpotPass = wifiPass.Text.Trim();
if (hotSpotPass.Length < 8)
{
MessageBox.Show("密码必须大于8位,请重新输入...");
wifiPass.Focus();
}
else
{
if (createHotSpot(hotSpotName, hotSpotPass))
MessageBox.Show("wifi热点创建成功\n热点名为:" + hotSpotName + "\n密码为:" + hotSpotPass);
else
MessageBox.Show("wifi热点创建失败");
}
}

/**
* 执行Cmd命令创建wifi热点
* */
public Boolean createHotSpot(string hotSpotName, string hotSpotPass)
{
//创建wifi热点三部曲,仅适用于win7下
string cmd1 = "netsh wlan set hostednetwork mode=allow";
string cmd2 = "netsh wlan set hostednetwork ssid=" + hotSpotName + " key=" + hotSpotPass;
string cmd3 = "netsh wlan start hostednetwork";

string[] cmd = new string[] { cmd1, cmd2, cmd3 };

string rs = execMutipleCmd(cmd);

cmdMsg.AppendText(rs);

return regexCheckIfSuccess(rs, "已启动承载网络");
}
/**
* 用正则匹配是否成功
* */
public Boolean regexCheckIfSuccess(string msg, string reg)
{
Regex r = new Regex(reg);
Match m = r.Match(msg);
if (m.Success)
return true;
else
return false;
}

private void stopWifi_Click(object sender, EventArgs e)
{
if (stopHotSpot())
MessageBox.Show("禁止wifi热点成功");
else
MessageBox.Show("禁止操作失败");
}

public Boolean stopHotSpot()
{
string cmd = "netsh wlan set hostednetwork mode=disallow";

string rs = execSingleCmd(cmd);
cmdMsg.AppendText(rs);
return regexCheckIfSuccess(rs, "承载网络模式已设置为禁止");
}
/**
* 执行单条命令
* */
public static string execSingleCmd(string cmd)
{
//因为cmd直接在window system32目录下,所以无需加路径
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");

//设置不显示cmd窗口
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;

startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;

Process process = Process.Start(startInfo);
process.StandardInput.AutoFlush = true;

process.StandardInput.WriteLine(cmd);
process.StandardInput.WriteLine("exit");
//等待程序执行完退出进程
process.WaitForExit();
//截获输出流
string rs = process.StandardOutput.ReadToEnd();
process.Close();
return rs;
}

/**
* 执行多条命令
* */
public static string execMutipleCmd(string[] cmd)
{
//因为cmd直接在window system32目录下,所以无需加路径
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");

//设置不显示cmd窗口
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;

startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;

Process process = Process.Start(startInfo);
process.StandardInput.AutoFlush = true;
for (int i = 0; i < cmd.Length; ++i)
process.StandardInput.WriteLine(cmd[i]);
process.StandardInput.WriteLine("exit");
//等待程序执行完退出进程
process.WaitForExit();
//截获输出流
string rs = process.StandardOutput.ReadToEnd();
process.Close();
return rs;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: