Skip to content

Instantly share code, notes, and snippets.

@blachniet
Last active April 18, 2023 13:58
Show Gist options
  • Save blachniet/3828142 to your computer and use it in GitHub Desktop.
Save blachniet/3828142 to your computer and use it in GitHub Desktop.
Open and close cd tray from C#
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