C#中調(diào)用命令行cmd開啟wifi熱點(diǎn)的實(shí)例代碼
要點(diǎn)1:cmd命令行的輸入命令
netsh wlan set hostednetwork mode=allow ssid=用戶名 key=密碼
netsh wlan start hostednetwork
netsh waln stop hostednetwork
netsh interface ip set address name="本地連接" source=dhcp
要點(diǎn)2:在C#中調(diào)用cmd.exe命令行
private void create(string str)
{
//process用于調(diào)用外部程序
System.Diagnostics.Process p = new System.Diagnostics.Process();
//調(diào)用cmd.exe
p.StartInfo.FileName = "cmd.exe";
//是否指定操作系統(tǒng)外殼進(jìn)程啟動(dòng)程序
p.StartInfo.UseShellExecute = false;
//可能接受來自調(diào)用程序的輸入信息
//重定向標(biāo)準(zhǔn)輸入
p.StartInfo.RedirectStandardInput = true;
//重定向標(biāo)準(zhǔn)輸出
p.StartInfo.RedirectStandardOutput = true;
//重定向錯(cuò)誤輸出
p.StartInfo.RedirectStandardError = true;
//不顯示程序窗口
p.StartInfo.CreateNoWindow = true;
//啟動(dòng)程序
p.Start();
//睡眠1s。
System.Threading.Thread.Sleep(1000);
//輸入命令
p.StandardInput.WriteLine(str);
//一定要關(guān)閉。
p.StandardInput.WriteLine("exit");
}
詳細(xì)的代碼如下:
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 wifi01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//“創(chuàng)建wifi熱點(diǎn)”按鈕
private void button1_Click(object sender, EventArgs e)
{
string str;
string userName = textBox1.Text;
string password = textBox2.Text;
if (password.Length >= 8 && userName != null)
{
// 命令行輸入命令,用來新建wifi
str="netsh wlan set hostednetwork mode=allow ssid="+userName+" key="+password;
create(str);
MessageBox.Show("新建了wifi熱點(diǎn)",
"新建成功",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
label4.Text = "新建了wifi熱點(diǎn)";
}
else
{
MessageBox.Show("你的賬號(hào)為空或你的密碼長度小于8",
"登陸失敗",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}
//"開啟wifi"按鈕
private void button2_Click(object sender, EventArgs e)
{
// 命令行輸入命令,
string str = "netsh wlan start hostednetwork";
create(str);
label4.Text = "已啟動(dòng)wifi熱點(diǎn)";
}
//“關(guān)閉wifi”按鈕
private void button3_Click(object sender, EventArgs e)
{
// 命令行輸入命令,
string str = "netsh wlan stop hostednetwork";
create(str);
label4.Text = "已關(guān)閉wifi熱點(diǎn)";
}
//在cmd控制臺(tái)輸入命令,
private void create(string str)
{
//process用于調(diào)用外部程序
System.Diagnostics.Process p = new System.Diagnostics.Process();
//調(diào)用cmd.exe
p.StartInfo.FileName = "cmd.exe";
//是否指定操作系統(tǒng)外殼進(jìn)程啟動(dòng)程序
p.StartInfo.UseShellExecute = false;
//可能接受來自調(diào)用程序的輸入信息
//重定向標(biāo)準(zhǔn)輸入
p.StartInfo.RedirectStandardInput = true;
//重定向標(biāo)準(zhǔn)輸出
p.StartInfo.RedirectStandardOutput = true;
//重定向錯(cuò)誤輸出
p.StartInfo.RedirectStandardError = true;
//不顯示程序窗口
p.StartInfo.CreateNoWindow = true;
//啟動(dòng)程序
p.Start();
//睡眠1s。
System.Threading.Thread.Sleep(1000);
//輸入命令
p.StandardInput.WriteLine(str);
//一定要關(guān)閉。
p.StandardInput.WriteLine("exit");
}
//自動(dòng)IP連接 按鈕
private void button4_Click(object sender, EventArgs e)
{
// 命令行輸入命令,用來自動(dòng)連接wifi:netsh interface ip set address name="本地連接" source=dhcp
string str="netsh interface ip set address name=\"本地連接\" source=dhcp";
string str1 = "銳捷是否提示你設(shè)置自動(dòng)獲取IP\n"+"或你想自動(dòng)獲取IP,請(qǐng)按確定";
DialogResult result = MessageBox.Show(str1,"自動(dòng)連接IP",
MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
create(str);
label4.Text = "銳捷自動(dòng)獲取IP";
}
}
}
}
- C# 執(zhí)行CMD命令并接收返回結(jié)果的操作方式
- C# 調(diào)用命令行執(zhí)行Cmd命令的操作
- C# 使用相同權(quán)限調(diào)用 cmd 傳入命令的方法
- C#隱式運(yùn)行CMD命令(隱藏命令窗口)
- C#調(diào)用CMD命令實(shí)例
- c#通過進(jìn)程調(diào)用cmd判斷登錄用戶權(quán)限代碼分享
- C# cmd中修改顯示(顯示進(jìn)度變化效果)的方法
- 通過C#調(diào)用cmd來修改服務(wù)啟動(dòng)類型
- 在asp.net(c#)下實(shí)現(xiàn)調(diào)用cmd的方法
- C#中隱式運(yùn)行CMD命令行窗口的方法
- C#程序調(diào)用cmd.exe執(zhí)行命令
相關(guān)文章
C#實(shí)現(xiàn)IP代理池調(diào)度的示例代碼
這篇文章主要為大家介紹了C#實(shí)現(xiàn)IP代理池調(diào)度的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的參考與學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-07-07使用GetInvalidFileNameCharts生成文件名
這篇文章主要介紹了一個(gè)很實(shí)用的函數(shù)Path.GetInvalidFileNameCharts(),他可以很方便的生成一個(gè)有效的文件名稱2014-01-01淺談C#下winform和JS的互相調(diào)用和傳參(webbrowser)
下面小編就為大家?guī)硪黄獪\談C#下winform和JS的互相調(diào)用和傳參(webbrowser)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12Unity Shader實(shí)現(xiàn)玻璃材質(zhì)效果
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)玻璃材質(zhì)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04c#判斷數(shù)據(jù)庫服務(wù)器是否已經(jīng)啟動(dòng)的方法
這篇文章主要介紹了使用c#判斷數(shù)據(jù)庫服務(wù)器是否已經(jīng)啟動(dòng)的方法,大家參考使用吧2014-01-01