diff options
Diffstat (limited to 'win/CS/HandBrakeWPF/Services/DriveDetectService.cs')
-rw-r--r-- | win/CS/HandBrakeWPF/Services/DriveDetectService.cs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/win/CS/HandBrakeWPF/Services/DriveDetectService.cs b/win/CS/HandBrakeWPF/Services/DriveDetectService.cs new file mode 100644 index 000000000..7f1b3f9b9 --- /dev/null +++ b/win/CS/HandBrakeWPF/Services/DriveDetectService.cs @@ -0,0 +1,89 @@ +// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="DriveDetectService.cs" company="HandBrake Project (http://handbrake.fr)">
+// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
+// </copyright>
+// <summary>
+// Drive Detection Helper.
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrakeWPF.Services
+{
+ using System;
+ using System.Management;
+
+ using HandBrakeWPF.Services.Interfaces;
+
+ /// <summary>
+ /// Drive Detection Helper.
+ /// </summary>
+ public class DriveDetectService : IDriveDetectService
+ {
+ /// <summary>
+ /// The watcher.
+ /// </summary>
+ private ManagementEventWatcher watcher;
+
+ /// <summary>
+ /// The detection action.
+ /// </summary>
+ private Action detectionAction;
+
+ /// <summary>
+ /// The start detection.
+ /// </summary>
+ /// <param name="action">
+ /// The detection Action.
+ /// </param>
+ public void StartDetection(Action action)
+ {
+ this.detectionAction = action;
+
+ var options = new ConnectionOptions { EnablePrivileges = true };
+ var scope = new ManagementScope(@"root\CIMV2", options);
+
+ try
+ {
+ var query = new WqlEventQuery
+ {
+ EventClassName = "__InstanceModificationEvent",
+ WithinInterval = TimeSpan.FromSeconds(1),
+ Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5" // DriveType - 5: CDROM
+ };
+
+ this.watcher = new ManagementEventWatcher(scope, query);
+ this.watcher.EventArrived += this.WatcherEventArrived;
+ this.watcher.Start();
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine(e.Message);
+ }
+ }
+
+ /// <summary>
+ /// The close.
+ /// </summary>
+ public void Close()
+ {
+ this.watcher.Stop();
+ }
+
+ /// <summary>
+ /// The watcher_ event arrived.
+ /// </summary>
+ /// <param name="sender">
+ /// The sender.
+ /// </param>
+ /// <param name="e">
+ /// The EventArrivedEventArgs.
+ /// </param>
+ private void WatcherEventArrived(object sender, EventArrivedEventArgs e)
+ {
+ if (this.detectionAction != null)
+ {
+ this.detectionAction();
+ }
+ }
+ }
+}
|