public static void EjectUSBDrive(string DriveLetterToEject) { VolumeDeviceClass volumeDeviceClass = new VolumeDeviceClass(); foreach (Volume device in volumeDeviceClass.Devices) { if (DriveLetterToEject == device.LogicalDrive) { //Eject if found matching drive letter device.Eject(false); break; } } }
void OnBackupWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { _lowerStatus.Text = "Finished"; syncProgressBar.Visible = false; cancelButton.Visible = false; closeButton.Visible = true; AcceptButton = closeButton; CancelButton = closeButton; cancelButton.Visible = false; closeButton.Focus(); Cursor = Cursors.Default; try { using (var x = new UsbEject.Library.VolumeDeviceClass()) { var device = x.Devices.FirstOrDefault(d => { Volume v = d as Volume; if (v == null) { return(false); } return(_destinationDeviceRoot.StartsWith(v.LogicalDrive)); }); if (device != null) { device.Eject(true); } else { _safeToRemoveLabel.Text = "No stick ejected"; } _safeToRemoveLabel.Visible = true; } } catch (Exception err) { Palaso.Reporting.ErrorReport.NotifyUserOfProblem(err, "Could not eject the usb memory stick."); } using (var player = new SoundPlayer(Properties.Resources.finished)) { player.Play(); } }
private void timer_Tick(object sender, EventArgs e) { try { btStart.Visible = !protectMe; btStopService.Visible = protectMe; lblState.Text = protectMe ? "running" : "stopped"; VolumeDeviceClass volumes = new VolumeDeviceClass(); foreach (Volume vol in volumes.Devices.Where(x => x.IsUsb == true)) { if (protectMe) { vol.Eject(false); } } } catch (Win32Exception ex) { log.Error("Error during fetching devices:"); log.Error(ex.Message, ex); } catch (Exception ex) { log.Error(ex.Message, ex); } }