/Rusty/Windows/Drive.cs

https://github.com/camerb/IronAHK · C# · 74 lines · 55 code · 13 blank · 6 comment · 2 complexity · 6fbd9eed243e444a413acc0ebd43d186 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. namespace IronAHK.Rusty.Windows
  7. {
  8. /// <summary>
  9. /// Implementation for native Windows Drive Operations
  10. /// </summary>
  11. class Drive : Common.Drive
  12. {
  13. static readonly string IOPathPrefix = @"\\.\";
  14. public Drive(DriveInfo drv)
  15. : base(drv) { }
  16. public override void Eject() {
  17. IntPtr fileHandle = IntPtr.Zero;
  18. try {
  19. // Create an handle to the drive
  20. fileHandle = WindowsAPI.CreateFile(this.CreateDeviceIOPath,
  21. WindowsAPI.GENERICREAD, 0, IntPtr.Zero,
  22. WindowsAPI.OPENEXISTING, 0, IntPtr.Zero);
  23. if((int)fileHandle != WindowsAPI.INVALID_HANDLE) {
  24. // Eject the disk
  25. uint returnedBytes;
  26. WindowsAPI.DeviceIoControl(fileHandle, WindowsAPI.IOCTL_STORAGE_EJECT_MEDIA,
  27. IntPtr.Zero, 0,
  28. IntPtr.Zero, 0,
  29. out returnedBytes,
  30. IntPtr.Zero);
  31. }
  32. } catch {
  33. throw new Exception(Marshal.GetLastWin32Error().ToString());
  34. } finally {
  35. // Close Drive Handle
  36. WindowsAPI.CloseHandle(fileHandle);
  37. fileHandle = IntPtr.Zero;
  38. }
  39. }
  40. public string CreateDeviceIOPath {
  41. get {
  42. return IOPathPrefix + drive.Name.Substring(0, 1) + ":";
  43. }
  44. }
  45. public override void Retract() {
  46. throw new NotImplementedException();
  47. }
  48. public override StatusCD Status {
  49. get { throw new NotImplementedException(); }
  50. }
  51. public override long Serial {
  52. get { throw new NotImplementedException(); }
  53. }
  54. public override void Lock() {
  55. throw new NotImplementedException();
  56. }
  57. public override void UnLock() {
  58. throw new NotImplementedException();
  59. }
  60. }
  61. }