2013-04-01 60 views
6

既に無効になっているカードを有効にする必要があり、WMI NetworkAdapterのサーチャーがオブジェクトを返さないという問題があります。C#を使用して無効なワイヤレスネットワークカードを有効にする方法

これを行う方法は考えられますが、動作させることができませんでした。つまり、これをコンストラクタ名として使用してmanagementObjectを作成することです。しかし、これは単に他の方法は、netshをシェルとの両方の、一種の醜いあるデバイスを、有効、またはShell32は/ DLLもう一度、同じことを行うには、「有効」を使用することでした例外

{\\.\root\CIMV2:Win32_NetworkAdapter.NetConnectionID='Wireless Network Connection'}

をスローしますちょうど名前を渡す。私はHKLM\SYSTEM\CurrentControlSet\Networkのレジストリスキャンから名前を取得していて、MediaType = 2を探してワイヤレスデバイスの文字列リストを取得しています。ワイヤレスデバイスのnetworkObjectを取得できるようにアダプタが有効な状態でアプリケーションを実行すると、すべてが良好ですが、ワイヤレスデバイスが無効な状態でアプリケーションが起動すると、すべてが終了します。

おかげ

編集:これは、この方法で私は仕事が大好きだコードが、なし、外出先:(

using System; 
using System.Management; 
class Sample 
{ 
    public static int Main(string[] args) 
    { 
     ManagementObject mObj = new ManagementObject("\\\\.\\root\\CIMV2:Win32_NetworkAdapter.NetConnectionID=\"Wireless Network Connection\""); 
     mObj.InvokeMethod("Enable", null); 
     return 0; 
    } 
} 

答えて

1

あり、本質的にWMIとWin32_NetworkAdapterクラスを活用するためにC#を使用していることを持っている必要があります。以下のための内蔵方法:

  • 無効に
  • を有効にします0

したがって、選択したインターフェイスでコマンドを実行できます。

あなたはこのようにそれを達成することができます

SelectQuery query = new SelectQuery("Win32_NetworkAdapter", "NetConnectionStatus=2"); 
ManagementObjectSearcher search = new ManagementObjectSearcher(query); 
foreach(ManagementObject result in search.Get()) 
{ 
    NetworkAdapter adapter = new NetworkAdapter(result); 

    // Identify the adapter you wish to disable here. 
    // In particular, check the AdapterType and 
    // Description properties. 

    // Here, we're selecting the LAN adapters. 
    if (adapter.AdapterType.Equals("Ethernet 802.3")) 
    { 
     adapter.Disable(); 
    } 
} 

blog that actually outlines such a taskがあります。 WMIクラスの周りにWrapperを作成する方法を定義します。

別のsolution may be to also useControlService(advapi32)。うまくいけば、これらのいずれかの方法は、返信用..

+1

感謝を助けること

[DllImport("advapi32.dll", SetLastError=true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ControlService(IntPtr hService, SERVICE_CONTROL dwControl, ref SERVICE_STATUS lpServiceStatus); 

!私は何をすることができないようです。無効になっているデバイスの検索で何も返されないため、無効になっているアダプタを有効にします。デバイスを無効にすることは本当に簡単ですが、オブジェクトを取得できないデバイスを有効にすることは、Imが問題を抱えていることです。理想的には、これが私が仕事に挑戦しようとしているものです。 – pedigree

+0

正確なインターフェイスを問い合わせると、 'enable'メソッドで実装することができます。私が試してみると、うまくいかないのですか? – Greg

+0

@Grey - 電話する前に無線を無効にしていますか(デバイスマネージャで)?もしそうなら、どんなOS/.NETをターゲットにしていますか? – pedigree

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(); 
        } 
       } 
      } 
+1

私は信じていますが、コードの説明はとても役に立ちます – olyv

関連する問題