C#中判断光驱

        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());
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值