日期:2014-05-18 浏览次数:20919 次
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace SystemDevices { class Program { static void Main(string[] args) { Guid classGuid = Guid.Empty; IntPtr hDevInfo = Win32.SetupDiGetClassDevs(ref classGuid, null, IntPtr.Zero, Win32.DIGCF_ALLCLASSES | Win32.DIGCF_PRESENT); if (hDevInfo.ToInt32() == Win32.INVALID_HANDLE_VALUE) { Console.WriteLine("访问硬件设备失败"); } else { int i = 0; StringBuilder deviceName = new StringBuilder(); deviceName.Capacity = Win32.MAX_DEV_LEN; do { SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA(); devInfoData.cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA)); devInfoData.classGuid = Guid.Empty; devInfoData.devInst = 0; devInfoData.reserved = IntPtr.Zero; bool result = Win32.SetupDiEnumDeviceInfo(hDevInfo, i, devInfoData); if (false == result) { break; } Console.WriteLine("Device: {0}", i); Console.WriteLine("\tGuid={0}", devInfoData.classGuid); Console.WriteLine("\tName={0}", Win32.GetClassNameFromGuid(devInfoData.classGuid)); Console.WriteLine("\tDescription={0}", Win32.GetClassDescriptionFromGuid(devInfoData.classGuid)); Console.WriteLine("\tInstance Id={0}", Win32.GetDeviceInstanceId(hDevInfo, devInfoData)); ++i; } while (true); Console.WriteLine("*****************************************************"); Console.Write("输入要禁止的设备号(0-{0}):", i-1); string str = Console.ReadLine(); int id; if (true == Int32.TryParse(str, out id)) { if (id >= 0) { Console.Write("输入\"Y\"启用该设备:"); str = Console.ReadLine(); Console.WriteLine("{0}设备{1}", (str.ToLower() == "y" ? "启用" : "禁用"), Win