C#中調用命令列cmd開啟wifi熱點的執行個體代碼

來源:互聯網
上載者:User

要點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

要點2:在C#中調用cmd.exe命令列

複製代碼 代碼如下: private void create(string str)
{
//process用於調用外部程式
System.Diagnostics.Process p = new System.Diagnostics.Process();
//調用cmd.exe
p.StartInfo.FileName = "cmd.exe";
//是否指定作業系統外殼進程啟動程式
p.StartInfo.UseShellExecute = false;
//可能接受來自調用程式的輸入資訊
//重新導向標準輸入
p.StartInfo.RedirectStandardInput = true;
//重新導向標準輸出
p.StartInfo.RedirectStandardOutput = true;
//重新導向錯誤輸出
p.StartInfo.RedirectStandardError = true;
//不顯示程式視窗
p.StartInfo.CreateNoWindow = true;
//啟動程式
p.Start();
//睡眠1s。
System.Threading.Thread.Sleep(1000);
//輸入命令
p.StandardInput.WriteLine(str);
//一定要關閉。
p.StandardInput.WriteLine("exit");
}

詳細的代碼如下:

複製代碼 代碼如下: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();
}
//“建立wifi熱點”按鈕
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熱點",
"建立成功",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
label4.Text = "建立了wifi熱點";
}
else
{
MessageBox.Show("你的帳號為空白或你的密碼長度小於8",
"登陸失敗",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}
//"開啟wifi"按鈕
private void button2_Click(object sender, EventArgs e)
{
// 命令列輸入命令,
string str = "netsh wlan start hostednetwork";
create(str);
label4.Text = "已啟動wifi熱點";
}
//“關閉wifi”按鈕
private void button3_Click(object sender, EventArgs e)
{
// 命令列輸入命令,
string str = "netsh wlan stop hostednetwork";
create(str);
label4.Text = "已關閉wifi熱點";
}
//在cmd控制台輸入命令,
private void create(string str)
{
//process用於調用外部程式
System.Diagnostics.Process p = new System.Diagnostics.Process();
//調用cmd.exe
p.StartInfo.FileName = "cmd.exe";
//是否指定作業系統外殼進程啟動程式
p.StartInfo.UseShellExecute = false;
//可能接受來自調用程式的輸入資訊
//重新導向標準輸入
p.StartInfo.RedirectStandardInput = true;
//重新導向標準輸出
p.StartInfo.RedirectStandardOutput = true;
//重新導向錯誤輸出
p.StartInfo.RedirectStandardError = true;
//不顯示程式視窗
p.StartInfo.CreateNoWindow = true;
//啟動程式
p.Start();
//睡眠1s。
System.Threading.Thread.Sleep(1000);
//輸入命令
p.StandardInput.WriteLine(str);
//一定要關閉。
p.StandardInput.WriteLine("exit");
}
//自動IP串連 按鈕
private void button4_Click(object sender, EventArgs e)
{
// 命令列輸入命令,用來自動連接wifi:netsh interface ip set address name="本地串連" source=dhcp
string str="netsh interface ip set address name=\"本地串連\" source=dhcp";
string str1 = "銳捷是否提示你設定自動擷取IP\n"+"或你想自動擷取IP,請按確定";
DialogResult result = MessageBox.Show(str1,"自動連接IP",
MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
create(str);
label4.Text = "銳捷自動擷取IP";
}

}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.