2009-12-29 83 views
1

Windows 7引入了虚拟WiFi,它允许您创建热点。但是我找不到任何有关在C#中执行的教程。我发现虚拟路由器(它是开源的,用C#编写的),但我似乎无法弄清楚它是如何工作的,因为它有很多不相关的代码,因为它是作为服务实现的。Windows 7虚拟WiFi使用C#?

任何人都可以解释如何创建一个热点并分配IP地址给客户端?我不需要像ICS这样的功能,但我希望能够通过广播网关和DNS信息。

还有一个闭源替代品叫做Connectify。我设法得到它的来源,但它没有多大帮助。它使用开源库,但我不知道如何使用它创建热点。

+0

为什么不只是安装虚拟路由器(MSI)? – 2009-12-29 21:08:46

+0

因为我需要修改它的几个方面+我需要一些作为应用程序运行而不是服务 – 2009-12-29 21:22:34

+2

你是如何得到Connectify的源代码的? – Nate 2009-12-30 20:07:00

回答

0

您是否想过研究这个Code-Plex项目Virtual Router

+0

或者你可以看到[MyRouter](http://myrouter.codeplex.com/) – 2012-06-28 07:01:26

1

既然您已经找到了一个完全符合您需要的项目,为何不理解该项目?

看起来你感兴趣的大部分代码都在“VirtualRouter.Wlan”项目中。从那里开始,如果你不明白,就尝试询问具体的问题。

0
 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; 
     using System.Diagnostics; 
     using System.Security.Principal; 

     namespace WifiRouter 
     { 
      public partial class Form1 : Form 
      { 
       bool connect = false; 
       public Form1() 
       { 

        InitializeComponent(); 
       } 

       public static bool IsAdmin() 
       { 
        WindowsIdentity id = WindowsIdentity.GetCurrent(); 
        WindowsPrincipal p = new WindowsPrincipal(id); 
        return p.IsInRole(WindowsBuiltInRole.Administrator); 
       } 
       public void RestartElevated() 
       { 
        ProcessStartInfo startInfo = new ProcessStartInfo(); 
        startInfo.UseShellExecute = true; 
        startInfo.CreateNoWindow = true; 
        startInfo.WorkingDirectory = Environment.CurrentDirectory; 
        startInfo.FileName = System.Windows.Forms.Application.ExecutablePath; 
        startInfo.Verb = "runas"; 
        try 
        { 
         Process p = Process.Start(startInfo); 
        } 
        catch 
        { 

        } 

        System.Windows.Forms.Application.Exit(); 
       } 
       private void button1_Click(object sender, EventArgs e) 
       { 
        string ssid = textBox1.Text, key = textBox2.Text; 
        if (!connect) 
        { 
         if (String.IsNullOrEmpty(textBox1.Text)) 
         { 
          MessageBox.Show("SSID cannot be left blank !", 
          "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
         } 
         else 
         { 

          if (textBox2.Text == null || textBox2.Text == "") 
          { 
           MessageBox.Show("Key value cannot be left blank !", 
           "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
          } 
          else 
          { 
           if (key.Length >= 6) 
           { 
            Hotspot(ssid, key, true); 
            textBox1.Enabled = false; 
            textBox2.Enabled = false; 
            button1.Text = "Stop"; 
            connect = true; 
           } 
           else 
           { 
            MessageBox.Show("Key should be more then or Equal to 6 Characters !", 
            "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
           } 
          } 
         } 
        } 
        else 
        { 
         Hotspot(null, null, false); 
         textBox1.Enabled = true; 
         textBox2.Enabled = true; 
         button1.Text = "Start"; 
         connect = false; 
        } 
       } 
       private void Hotspot(string ssid, string key,bool status) 
       { 
        ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe"); 
        processStartInfo.RedirectStandardInput = true; 
        processStartInfo.RedirectStandardOutput = true; 
        processStartInfo.CreateNoWindow = true; 
        processStartInfo.UseShellExecute = false; 
        Process process = Process.Start(processStartInfo); 

        if (process != null) 
        { 
         if (status) 
         { 
          process.StandardInput.WriteLine("netsh wlan set hostednetwork mode=allow ssid=" + ssid + " key=" + key); 
          process.StandardInput.WriteLine("netsh wlan start hosted network"); 
          process.StandardInput.Close(); 
         } 
         else 
         { 
          process.StandardInput.WriteLine("netsh wlan stop hostednetwork"); 
          process.StandardInput.Close(); 
         } 
        } 
       } 

       private void Form1_Load(object sender, EventArgs e) 
       { 
        if (!IsAdmin()) 
        { 
         RestartElevated(); 
        } 
       } 

       private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
       { 
        Hotspot(null, null, false); 
        Application.Exit(); 
       } 
      } 
     }