using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace startORstop
{
internal class Program
{
static void Main(string[] args)
{
GetDevices();
DeviceDisable();
Thread.Sleep(3000);
DeviceEnable();
}
//用于存放获取到的设备
static ManagementObject USB = null;
//获取需要设备
public static void GetDevices()
{
//根据某个条件查找到对应设备
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM win32_PnPEntity");
//遍历所有设备信息
foreach (ManagementObject mgt in searcher.Get())
{
//Console.WriteLine(mgt);
//按名字筛选
if (Convert.ToString(mgt).Contains("5&16782678&0&8"))
{
USB = mgt;
}
}
//Console.ReadKey();
}
//启用方法
public static void DeviceEnable()
{
if (USB == null)
{
return;
}
try
{
USB.InvokeMethod("Enable", null);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
//禁用方法
public static void DeviceDisable()
{
if (USB == null)
{
return;
}
try
{
USB.InvokeMethod("Disable", null);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}