Kopal in the direction of DriveInfo , but comparing two arrays is not a very elegant solution.

    2 answers 2

    You can use WMI technology.

    Connect to the project assembly System.Management.dll.

    Write the following code:

     using System.Management; var watcher = new ManagementEventWatcher(); var query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2"); watcher.Query = query; watcher.EventArrived += Watcher_EventArrived; watcher.Query = query; watcher.Start(); private static void Watcher_EventArrived(object sender, EventArrivedEventArgs e) { var driveName = e.NewEvent.Properties["DriveName"].Value.ToString(); Console.WriteLine(driveName); } 

    In the event we get a drive letter.

    The ManagementEventWatcher object is Disposable, so it is highly desirable to free up resources by calling Dispose (or use using if possible) after you finish working with it.

    More details can be found here .

      I can suggest to listen to notifications from Windows - WM_DEVICECHANGE . The system sends notification of hardware configuration changes among devices.

      Specifically, you will be useful:

      DBT_DEVICEARRIVAL - sent after inserting a device or media. The program will receive this message when the device is ready for use.

      DBT_DEVICEQUERYREMOVE - sent when the system requests permission to remove a device or media. Any application can reject this request and cancel the deletion.

      DBT_DEVICEREMOVECOMPLETE - sent after the device has been removed. When your program receives this event, the device is no longer available.

      An example of a project on this principle:

      https://www.codeproject.com/Articles/18062/Detecting-USB-Drive-Removal-in-aC-Program

      Learn more about WM_DEVICECHANGE :

      https://msdn.microsoft.com/ru-ru/library/windows/desktop/aa363480 (v=vs.85).aspx

      • four
        It would not be bad to add some code to the answer. Links will die and more than 50% of the informativeness of your answer along with them. - Bulson