Last active
April 18, 2023 13:58
-
-
Save blachniet/3828142 to your computer and use it in GitHub Desktop.
Open and close cd tray from C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
namespace AttackoftheTray | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ConsoleKey key; | |
while (true) | |
{ | |
key = Console.ReadKey().Key; | |
switch (key) | |
{ | |
case ConsoleKey.O: | |
open(); | |
break; | |
case ConsoleKey.C: | |
close(); | |
break; | |
default: | |
return; | |
} | |
} | |
} | |
private static void open() | |
{ | |
int ret = mciSendString("set cdaudio door open", null, 0, IntPtr.Zero); | |
} | |
private static void close() | |
{ | |
int ret = mciSendString("set cdaudio door closed", null, 0, IntPtr.Zero); | |
} | |
[DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi)] | |
protected static extern int mciSendString(string lpstrCommand, | |
StringBuilder lpstrReturnString, | |
int uReturnLength, | |
IntPtr hwndCallback); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment