我需要我的程序只能使用某些USB闪存驱动器(来自单个制造商)而忽略所有其他USB闪存驱动器(来自任何其他制造商).
是否可以使用.NET 2.0检查Windows上是否插入了特定的USB卡?怎么样?
如果我通过WMI找到它,我可以以某种方式确定USB驱动器所在的驱动器号吗?
编辑:添加了打印驱动器号的代码.
检查此示例是否适合您.它使用WMI.
Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]); ... Console.WriteLine(" Name: {0}", c["Name"]); // here it will print drive letter
完整的代码示例:
namespace WMISample { using System; using System.Management; public class MyWMIQuery { public static void Main() { try { ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskDrive"); foreach (ManagementObject queryObj in searcher.Get()) { Console.WriteLine("DeviceID: {0}", queryObj["DeviceID"]); Console.WriteLine("PNPDeviceID: {0}", queryObj["PNPDeviceID"]); Console.WriteLine("Manufacturer: {0}", queryObj["Manufacturer"]); Console.WriteLine("Model: {0}", queryObj["Model"]); foreach (ManagementObject b in queryObj.GetRelated("Win32_DiskPartition")) { Console.WriteLine(" Name: {0}", b["Name"]); foreach (ManagementBaseObject c in b.GetRelated("Win32_LogicalDisk")) { Console.WriteLine(" Name: {0}", c["Name"]); // here it will print drive letter } } // ... Console.WriteLine("--------------------------------------------"); } } catch (ManagementException e) { Console.WriteLine(e.StackTrace); } Console.ReadLine(); } } }
我认为这些属性可以帮助您区分正版USB驱动器与其他驱动器.使用多个笔式驱动器进行测试,以检查值是否相同.请在此处查看Win32_DiskDrive属性的完整参考:
http://msdn.microsoft.com/en-us/library/aa394132(VS.85).aspx
检查这篇文章是否对您有任何帮助:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/48a9758c-d4db-4144-bad1-e87f2e9fc979