Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在windows 8.1上使用c#启用和禁用usb设备是否有API更改?_C#_Windows_Winapi - Fatal编程技术网

在windows 8.1上使用c#启用和禁用usb设备是否有API更改?

在windows 8.1上使用c#启用和禁用usb设备是否有API更改?,c#,windows,winapi,C#,Windows,Winapi,我想将启用usb的设备从启用切换到禁用,反之亦然。 我使用NetworkAdapter api在Windows7中切换usb,效果很好。但是,当我在windows 8.1中使用相同的代码时,即使api禁用了我的usb设备,更改也不会反映在设备管理器中。一旦我弹出并重新连接usb,我可以确认设备确实被禁用。 我想知道windows 8.1是否有我可能不知道的api更改。如果不是,我错在哪里 我的代码是: switch (requiredstatus) { case "Disable":

我想将启用usb的设备从启用切换到禁用,反之亦然。 我使用NetworkAdapter api在Windows7中切换usb,效果很好。但是,当我在windows 8.1中使用相同的代码时,即使api禁用了我的usb设备,更改也不会反映在设备管理器中。一旦我弹出并重新连接usb,我可以确认设备确实被禁用。 我想知道windows 8.1是否有我可能不知道的api更改。如果不是,我错在哪里

我的代码是:

switch (requiredstatus)
  {
    case "Disable":
       SelectQuery query = new SelectQuery("Win32_NetworkAdapter","NetConnectionStatus=2");
       ManagementObjectSearcher search = new ManagementObjectSearcher(query);
       foreach (ManagementObject device in search.Get())
         {
          NetworkAdapter adapter = new NetworkAdapter(device);

          // Identify the adapter you wish to disable here. 
          // Here, we're selecting the LAN adapters.
          if (adapter.Name.Contains("Device1"))
            {
              //Console.WriteLine("Disable");
               adapter.Disable();
               result = true;
            }
    }
  break;
  case "Enable":
     SelectQuery query1 = new SelectQuery("Win32_NetworkAdapter","NetConnectionStatus=0");
     ManagementObjectSearcher search1 = new ManagementObjectSearcher(query1);
     foreach (ManagementObject device in search1.Get())
       {
        NetworkAdapter adapter = new NetworkAdapter(device);

         // Identify the adapter you wish to disable here. 
         // Here, we're selecting the LAN adapters.
         if (adapter.Name.Contains("Device1"))
            {
             //Console.WriteLine("Enable");
              adapter.Enable();
              result = true;
             }
        }
  break;
 } 

解决方案非常简单!Windows-8、Windows-8.1需要管理员权限才能对网络适配器进行任何更改。因此,如果我们以管理员的身份显式运行这段代码,问题就会得到解决。