Feedback

C# - CD/DVD-Laufwerk öffnen bzw. schließen

Veröffentlicht von am 7/22/2009
(2 Bewertungen)
Mit dieser Klasse kann man ein oder sogar mehrere CD/DVD-Laufwerke öffnen bzw. schließen.

Beispiel:

public class Program
{
public static void Main()
{
OpenOrCloseCDDrive openOrClose = new OpenOrCloseCDDrive();
List<DriveInfo> drives = openOrClose.GetCDDrives;
openOrClose.Close(drives[0]);
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

public class OpenOrCloseCDDrive
{
    [DllImport("winmm.dll", CharSet = CharSet.Auto, EntryPoint = "mciSendString")]
    public static extern int MciSendString(string command,
       StringBuilder buffer, int bufferSize, IntPtr hwndCallback);

    protected const int IntMciSuccess = 0;
    protected const int IntBufferSize = 127;

    protected List<DriveInfo> listCDDrives = new List<DriveInfo>();

    public List<DriveInfo> GetCDDrives
    {
        get
        {
            return listCDDrives;
        }
    }

    public OpenOrCloseCDDrive()
    {
        DriveInfo[] drives = DriveInfo.GetDrives();

        foreach (DriveInfo drive in drives)
        {
            if (drive.DriveType == DriveType.CDRom)
            {
                listCDDrives.Add(drive);
            }
        }
    }

    public void Open(DriveInfo cdDrive)
    {
        if (cdDrive.DriveType != DriveType.CDRom)
        {
            throw new InvalidOperationException
                ("Der übergebene Parameter enthält kein gültiges CD/DVD-Laufwerk");
        }

        StringBuilder buffer = new StringBuilder();

        int errorCode = MciSendString
            (
            (
            String.Format
            ("set CDAudio!{0} door open", cdDrive.Name)
            ),
            buffer,
            IntBufferSize,
            IntPtr.Zero
            );
    }

    public void Close(DriveInfo cdDrive)
    {
        if (cdDrive.DriveType != DriveType.CDRom)
        {
            throw new InvalidOperationException
                ("Der übergebene Parameter enthält kein gültiges CD/DVD-Laufwerk");
        }

        StringBuilder buffer = new StringBuilder();

        int errorCode = MciSendString
            (
            (
            String.Format
            ("set CDAudio!{0} door closed", cdDrive.Name)
            ),
            buffer,
            IntBufferSize,
            IntPtr.Zero
            );
    }
}
Abgelegt unter CD, DVD, Schublade, öffnen, Laufwerk.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!