|
2 | 2 | using System.ComponentModel; |
3 | 3 | using System.IO; |
4 | 4 | using System.Runtime.InteropServices; |
| 5 | +using System.Threading; |
5 | 6 |
|
6 | 7 | namespace ejectcd |
7 | 8 | { |
8 | 9 | public static class Program |
9 | 10 | { |
10 | 11 | public static int Main(string[] args) |
11 | 12 | { |
12 | | - DriveInfo[] drives; |
| 13 | + bool result; |
| 14 | + Mutex m = new Mutex(true, "ejectcd", out result); |
13 | 15 |
|
14 | | - try |
15 | | - { |
16 | | - drives = DriveInfo.GetDrives(); |
17 | | - } |
18 | | - catch (IOException e) |
| 16 | + if (!result) |
19 | 17 | { |
20 | | - Console.Error.WriteLine(e.Message); |
21 | | - return 1; |
22 | | - } |
23 | | - catch (UnauthorizedAccessException e) |
24 | | - { |
25 | | - Console.Error.WriteLine(e.Message); |
| 18 | + Console.Error.Write("Another instance is already running."); |
26 | 19 | return 1; |
27 | 20 | } |
28 | 21 |
|
29 | | - foreach (DriveInfo drive in drives) |
| 22 | + try |
30 | 23 | { |
31 | | - if (drive.DriveType != DriveType.CDRom) |
32 | | - continue; |
33 | | - |
34 | | - Console.WriteLine("Eject CD-ROM drive {0}.", drive.Name); |
35 | | - |
36 | | - IntPtr hDrive = new IntPtr(INVALID_HANDLE_VALUE); |
| 24 | + DriveInfo[] drives; |
37 | 25 |
|
38 | 26 | try |
39 | 27 | { |
40 | | - // Open the device |
41 | | - hDrive = CreateFile(@"\\.\" + drive.Name[0] + ':', FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, |
42 | | - IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero); |
43 | | - |
44 | | - if ((int)hDrive == INVALID_HANDLE_VALUE) { throw new Win32Exception(); } |
45 | | - |
46 | | - // Try and eject |
47 | | - int dummy = 0; |
48 | | - bool ejected = DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, |
49 | | - IntPtr.Zero, 0, ref dummy, IntPtr.Zero); |
50 | | - |
51 | | - if (!ejected) { throw new Win32Exception(); } |
52 | | - |
| 28 | + drives = DriveInfo.GetDrives(); |
| 29 | + } |
| 30 | + catch (IOException e) |
| 31 | + { |
| 32 | + Console.Error.WriteLine(e.Message); |
| 33 | + return 1; |
53 | 34 | } |
54 | | - catch (Win32Exception e) |
| 35 | + catch (UnauthorizedAccessException e) |
55 | 36 | { |
56 | 37 | Console.Error.WriteLine(e.Message); |
57 | 38 | return 1; |
58 | 39 | } |
59 | | - finally |
| 40 | + |
| 41 | + foreach (DriveInfo drive in drives) |
60 | 42 | { |
61 | | - if (hDrive.ToInt32() != INVALID_HANDLE_VALUE) |
62 | | - CloseHandle(hDrive); |
| 43 | + if (drive.DriveType != DriveType.CDRom) |
| 44 | + continue; |
| 45 | + |
| 46 | + Console.WriteLine("Eject CD-ROM drive {0}.", drive.Name); |
| 47 | + |
| 48 | + IntPtr hDrive = new IntPtr(INVALID_HANDLE_VALUE); |
| 49 | + |
| 50 | + try |
| 51 | + { |
| 52 | + // Open the device |
| 53 | + hDrive = CreateFile(@"\\.\" + drive.Name[0] + ':', FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, |
| 54 | + IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero); |
| 55 | + |
| 56 | + if ((int)hDrive == INVALID_HANDLE_VALUE) { throw new Win32Exception(); } |
| 57 | + |
| 58 | + // Try and eject |
| 59 | + int dummy = 0; |
| 60 | + bool ejected = DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, |
| 61 | + IntPtr.Zero, 0, ref dummy, IntPtr.Zero); |
| 62 | + |
| 63 | + if (!ejected) { throw new Win32Exception(); } |
| 64 | + |
| 65 | + } |
| 66 | + catch (Win32Exception e) |
| 67 | + { |
| 68 | + Console.Error.WriteLine(e.Message); |
| 69 | + return 1; |
| 70 | + } |
| 71 | + finally |
| 72 | + { |
| 73 | + if (hDrive.ToInt32() != INVALID_HANDLE_VALUE) |
| 74 | + CloseHandle(hDrive); |
| 75 | + } |
63 | 76 | } |
| 77 | + |
| 78 | + Console.WriteLine("Done."); |
| 79 | + } |
| 80 | + finally |
| 81 | + { |
| 82 | + m.ReleaseMutex(); |
| 83 | + m.Dispose(); |
64 | 84 | } |
65 | 85 |
|
66 | | - Console.WriteLine("Done."); |
67 | 86 | return 0; |
68 | 87 | } |
69 | 88 |
|
|
0 commit comments