2016-04-25 3 views
4

新しく挿入されたUSBドライブとそのドライブ文字を見つけるためのC#プログラムを作成しました。今私がこのプログラムを実行すると、挿入イベントが発生し、ドライブ文字を取得できませんでした。誰も私にこれをするアイデアを提案することはできますか?新たに挿入されたUSBドライブ文字をC#で取得するにはどうすればよいですか?

コード

static void Main(string[] args) 
{ 
    ManagementEventWatcher mwe_creation; //Object creation for 'ManagementEventWatcher' class is used to listen the temporary system event notofications based on specific query. 
    WqlEventQuery q_creation = new WqlEventQuery(); //Represents WMI(Windows Management Instrumentation) event query in WQL format for more information goto www.en.wikipedia.org/wiki/WQL 
    q_creation.EventClassName = "__InstanceCreationEvent";// Sets the eventclass to the query 
    q_creation.WithinInterval = new TimeSpan(0, 0, 2); // Setting up the time interval for the event check(here, it is 2 Seconds) 
    q_creation.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition'"; //Sets which kind of event to be notified 
    mwe_creation = new ManagementEventWatcher(q_creation); //Initializing new instance 
    mwe_creation.EventArrived += new EventArrivedEventHandler(USBEventArrived_Creation);//Calling up 'USBEventArrived_Creation' method when the usb storage plug-in event occured 
    mwe_creation.Start(); // Starting to listen to the system events based on the given query 
    while (true) ; 

} 
static void USBEventArrived_Creation(object sender, EventArrivedEventArgs e){ 

    Console.WriteLine("USB PLUGGED IN!"); 
    ManagementBaseObject instance = (ManagementBaseObject)e.NewEvent["TargetInstance"]; 
    foreach (var property in instance.Properties) 
    { 

     if (property.Name == "Name") 
      Console.WriteLine(property.Name + " = " + property.Value); 
    } 

} 
+0

は、あなたがこのVARドライブ=のDriveInfo.GetDrives() .Where(ドライブを試してみました:

マイクロソフトは、あなただけのステファンモジュール全体をインポートしたくない場合は、ポートすることができますVBのコード例を提供しました=> drive.IsReady && drive.DriveType == DriveType.Removable); – rashfmnb

+0

これは、システムに存在するすべてのリムーバブルドライブを選択します –

+0

あまりにも多くの情報を持っているこのsatckoverflowリンクを見てくださいhttp://stackoverflow.com/questions/6003822/how-to-detect-a-usb-drive- has-been-plug-in – rashfmnb

答えて

0

あなたは、既存のsoultionを再作成するために働きすぎていてもよいです。ここでは、必要な正確にインタフェースを提供し、十分に文書化していると思わStephen Mcohnによって行われた公開ライセンスプロジェクトは次のとおりです。ここで

http://www.codeproject.com/Articles/63878/Enumerate-and-Auto-Detect-USB-Drives

は、彼はちょうどUSBドライブ

new ManagementObjectSearcher(
     "select DeviceID, Model from Win32_DiskDrive " + 
     "where InterfaceType='USB'").Get()) 
についてフィルタリングする方法であります

特定のドライブ文字が復元された方法は次のとおりです。

特定のドライブの取得文字はAssociatorsを使用して達成され、Win32_LogicalDiskを取得し、デバイスIDを与えることができる。ドライブレター)。

ComputerName = "." 
Set wmiServices = GetObject (_ 
    "winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName) 
' Get physical disk drive 
Set wmiDiskDrives = wmiServices.ExecQuery ("SELECT Caption, DeviceID FROM Win32_DiskDrive") 

For Each wmiDiskDrive In wmiDiskDrives 
    WScript.Echo "Disk drive Caption: " & wmiDiskDrive.Caption & VbNewLine & "DeviceID: " & " (" & wmiDiskDrive.DeviceID & ")" 

    'Use the disk drive device id to 
    ' find associated partition 
    query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _ 
     & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"  
    Set wmiDiskPartitions = wmiServices.ExecQuery(query) 

    For Each wmiDiskPartition In wmiDiskPartitions 
     'Use partition device id to find logical disk 
     Set wmiLogicalDisks = wmiServices.ExecQuery _ 
      ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _ 
      & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition") 

     For Each wmiLogicalDisk In wmiLogicalDisks 
      WScript.Echo "Drive letter associated" _ 
       & " with disk drive = " _ 
       & wmiDiskDrive.Caption _ 
       & wmiDiskDrive.DeviceID _ 
       & VbNewLine & " Partition = " _ 
       & wmiDiskPartition.DeviceID _ 
       & VbNewLine & " is " _ 
       & wmiLogicalDisk.DeviceID 
     Next  
    Next 
Next 
関連する問題