CD-ROM ınızı Açıp Kapatma
BU örnekte cd-rom u açıp kapatmayı göreceğiz. İlk önce formunuza Class ekleyin. Class a aşağıdaki kodları yazın. Class kısmına
using System.Runtime.InteropServices; eklemeyi unutmayın.
Class a yazılacak kodlar
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
class Class1
{
[DllImport("winmm.dll", EntryPoint = "mciSendStringA")]
public static extern int mciSendString(string lpstrCommand,
string lpstrReturnString, int uReturnLength, int hwndCallback);
[DllImport("kernel32.dll", EntryPoint = "GetVolumeInformationA")]
public static extern int GetVolumeInformation(string lpRootPathName,
StringBuilder lpVolumeNameBuffer, int nVolumeNameSize,
int lpVolumeSerialNumber, int lpMaximumComponentLength,
int lpFileSystemFlags, string lpFileSystemNameBuffer,
int nFileSystemNameSize);
[DllImport("kernel32.dll", EntryPoint = "GetDriveTypeA")]
public static extern int GetDriveType(string nDrive);
}
}
Forma yazacağınız kodlar
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int i;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (i == Class1.mciSendString("set CDAudio door closed", null, 127, 0))
{
Class1.mciSendString("set CDAudio door open", null, 127, 0);
//MessageBox.Show("CD-ROM şu an açık");
button1.Text = "CD-ROM şu an açık ";
}
else if (i == Class1.mciSendString("set CDAudio door open", null, 127, 0))
{
Class1.mciSendString("set CDAudio door closed", null, 127, 0);
//MessageBox.Show("CD-ROM şu an açık");
button1.Text = "CD-ROM şu an kapalı ";
}
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "CD-ROM Aç/Kapa";
}
}
}
//Bir sonraki makalede görüşmek üzere...Bahadır