前言:
本人常年使用Windows 7(雖然在努力學習Ubuntu,但是必須承認Windows 7上擁有大量的優秀軟件,比如Evernote、Microsoft Office等)。但是由於學校的網絡是通過綁定MAC和IP地址,然后客戶端登錄的模式提供網絡服務的,所以同一時間只能夠允許一台設備上網(學校的客戶端會檢測路由器,一旦檢測到就會斷開連接)。
這時候Windows 7的虛擬WiFi就成了手機和平板電腦的救命稻草。虛擬WiFi需要一定的硬件支持,但是現在的筆記本一般都沒問題,本文不再討論了。虛擬WiFi開啟的方式一般有兩種:(1)使用類似Connectify的工具軟件。(2)使用命令提示符進行設置。本文介紹筆者的一個用來設置虛擬WiFi的小程序,其關鍵就是對命令提示符的二次調用。
開啟虛擬WiFi:
(一)工具軟件
本人用過的軟件只有Connectify。不得不說這是一款很好的軟件,界面友好、功能齊全。但是在本人的筆記本上工作卻不是很正常,打開初始化的過程非常卡(Y470 Win7 x64)。所以后來我一般選擇在命令提示符中手動開啟。
(二)命令提示符
首先介紹一下在命令提示符下開啟虛擬WiFi:
1、打開命令提示符cmd。(注意需要用管理員權限)
2、開啟虛擬WiFi: netsh wlan set hostednetwork mode=allow ssid=NameString key=KeyString(mode=disallow可以禁用虛擬WiFi,ssid表示熱點名稱,key則為連接到熱點的密碼)
3、設置連接源:打開控制面板,按照 控制面板 => 網絡和 Internet => 網絡連接的順序打開,你會發現多了一個Microsoft Virtual WiFi Miniport Adapter。按照你的信號源(如果連有線的就選本地連接,連無線的就選無線網絡連接),右鍵屬性 => 共享 勾選第一個選項,然后在下拉列表中選擇那個多出來的無線網絡連接。
4、開啟虛擬WiFi: netsh wlan start hostednetwork
5、關閉虛擬WiFi: netsh wlan stop hostednetwork
可以發現,如果每次開啟和關閉虛擬WiFi都要輸入那么長一串命令的確很麻煩,一個簡便的處理方式是將命令作為批處理方式。將這兩個命令分別寫入到txt文件中,然后將后綴名改為bat,批處理文件就做好了。
自己動手,豐衣足食:
事實上,用兩個批處理文件的確可以了,但是把兩個bat文件放在桌面上不嫌丑嗎?再說被人蹭網也是常事,如果要改密碼那么就必須重新修改命令,還是有點麻煩。於是本着計算機專業學生的二愣子精神,我打算自己動手做一個小程序來完成虛擬WiFi的開啟、關閉、修改、禁用等功能。
其實吧,作為一個半桶水本人也不會修改操作系統的一些參數。於是我就打算在cmd外面加一層殼,用WindowsForm做一個cmd的二次調用。首先是生成界面:
窗體上的兩個輸入框用來在更改和設置虛擬WiFi的SSID和連接密碼。顯示信息可以顯示出連接的連接設備數目等信息。開啟和關閉分別對應start和stop命令,禁用會刪除虛擬的無線連接。最后說說這個輸出面板,這是用來顯示cmd中的輸入信息,但是不能從這里輸入。
程序代碼 :
最重要也是實現的關鍵——對CMD的調用:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Diagnostics;
6: using System.IO;
7:
8: namespace VirtualWiFi
9: {
10: class Command
11: {
12: public static string RunCMD(List<string> commands)
13: {
14: System.Diagnostics.Process p = new System.Diagnostics.Process();
15: //設定程序名;
16: p.StartInfo.FileName = "cmd.exe";
17: //關閉shell的使用;
18: p.StartInfo.UseShellExecute = false;
19: //cmd窗口不顯示;
20: p.StartInfo.CreateNoWindow = true;
21: //重定向標准輸入、輸出、錯誤輸出;
22: p.StartInfo.RedirectStandardError = true;
23: p.StartInfo.RedirectStandardInput = true;
24: p.StartInfo.RedirectStandardOutput = true;
25:
26: p.Start();
27: foreach (string command in commands)
28: {
29: p.StandardInput.WriteLine(command);
30: }
31: p.StandardInput.WriteLine("exit");
32:
33: //讀取返回信息;
34: return p.StandardOutput.ReadToEnd();
35: }
36: }
37: }
然后是窗體的事件處理程序:
1: using System;
2: using System.Collections.Generic;
3: using System.Windows.Forms;
4:
5: namespace VirtualWiFi
6: {
7: public partial class Form1 : Form
8: {
9: private readonly string allow;
10: private readonly string disallow;
11: private readonly string show;
12: private readonly string start;
13: private readonly string stop;
14:
15: public Form1()
16: {
17: InitializeComponent();
18: show = @"netsh wlan show hostednetwork";
19: start = @"netsh wlan start hostednetwork";
20: stop = @"netsh wlan stop hostednetwork";
21: allow = @"netsh wlan set hostednetwork mode=allow";
22: disallow = @"netsh wlan set hostednetwork mode=disallow";
23: }
24:
25: private void buttonOK_Click(object sender, EventArgs e)
26: {
27: if (textBox1.Text == null || textBox1.Text.Length == 0 ||
28: textBox2.Text == null || textBox2.Text.Length == 0)
29: {
30: MessageBox.Show("SSID以及密碼不可為空!");
31: return;
32: }
33: string setting = allow + " ssid=" + textBox1.Text + " key=" + textBox2.Text;
34: var commands = new List<string>();
35: commands.Add(setting);
36: commands.Add(show);
37: Command.RunCMD(commands);
38: }
39:
40: private void radioButtonStart_CheckedChanged(object sender, EventArgs e)
41: {
42: var commands = new List<string>();
43: commands.Add(start);
44: textBox3.Text = Command.RunCMD(commands);
45: }
46:
47: private void radioButtonStop_CheckedChanged(object sender, EventArgs e)
48: {
49: var commands = new List<string>();
50: commands.Add(stop);
51: textBox3.Text = Command.RunCMD(commands);
52: }
53:
54: private void buttonDisplay_Click(object sender, EventArgs e)
55: {
56: var commands = new List<string>();
57: commands.Add(show);
58: textBox3.Text = Command.RunCMD(commands);
59: }
60:
61: private void buttonForbidden_Click(object sender, EventArgs e)
62: {
63: var commands = new List<string>();
64: commands.Add(disallow);
65: textBox3.Text = Command.RunCMD(commands);
66: }
67: }
68: }