C#中調用命令行程序開啓wifi熱點

最近想在win7上開啓wifi熱點,因而就弄出下面這個小東西,裏面涉及如何在控制檯上輸入命令,分享一下。首先在VS中建立一個window窗口,而後建立兩個四個button,兩個輸入框。如圖所示: spa


要點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");
        }


命令行

詳細的代碼以下:
orm

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";
            }
            
        }
    }
}
相關文章
相關標籤/搜索