public string myCDROM;
// API申明
[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);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 获取屏幕的宽,高
int xWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
int yHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
// 获得逻辑驱动器列表数组
string [] drives = System.IO.Directory.GetLogicalDrives();
for (int i = 0; i < drives.Length; i++)
{
// 取得每个驱动器盘符
string a = drives[i].ToString();
// 根据盘符转换成驱动器类型
int b = GetDriveType(drives[i]);
if (b == 2)
{
MessageBox.Show(b.ToString() + " , 软驱", a);
}
if (b == 3)
{
MessageBox.Show(b.ToString() + " , 逻辑硬盘", a);
}
if (b == 5)
{
MessageBox.Show(b.ToString() + " , 光驱", a);
// 通过查找光驱中的指定文件来判断当前正在使用哪个光驱
Boolean trueFile = new Boolean();
trueFile = false;
trueFile = System.IO.File.Exists( a +"math.exe"); // 根据Exists的返回值判断
if (trueFile == true)
{
myCDROM = a;
//MessageBox.Show( "当前使用的光驱是:" + myCDROM);
this.axShockwaveFlash1.Movie = myCDROM + "//content//start.swf";
this.axShockwaveFlash2.Movie = myCDROM + "//content//main.swf";
return;
}
}
}
Microsoft Visual C# 2005 Express Edition中直接使用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
System.IO.DriveInfo[] drives1 = System.IO.DriveInfo.GetDrives();
for (int i = 0; i < drives1.Length; i++)
{
MessageBox.Show("驱动器:" + drives1[i].ToString() + ",类型:" + drives1[i].DriveType.ToString() + ",硬件信息码:" + drives1[i].DriveType.GetHashCode().ToString());
}
}
}
}