C#检测pc光驱里是否插入了光盘的方法

本文实例讲述了C#检测pc光驱里是否插入了光盘的方法。分享给大家供大家参考。具体如下:

C# 检测pc光驱里是否插入了光盘,需要添加System.Management.dll 的引用

using System;
using System.Management;
namespace CDROMManagement
{
 class WMIEvent
 {
  static void Main(string[] args)
  {
   WMIEvent we = new WMIEvent();
   ManagementEventWatcher w = null;
   WqlEventQuery q;
   ManagementOperationObserver observer = new ManagementOperationObserver();
   // Bind to local machine
   ConnectionOptions opt = new ConnectionOptions();
   opt.EnablePrivileges = true; //sets required privilege
   ManagementScope scope = new ManagementScope( "root\\CIMV2", opt );
   try
   {
    q = new WqlEventQuery();
    q.EventClassName = "__InstanceModificationEvent";
    q.WithinInterval = new TimeSpan( 0, 0, 1 );
    // DriveType - 5: CDROM
    q.Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5";
    w = new ManagementEventWatcher( scope, q );
    // register async. event handler
    w.EventArrived += new EventArrivedEventHandler( we.CDREventArrived );
    w.Start();
    // Do something usefull,block thread for testing
    Console.ReadLine();
   }
   catch( Exception e )
   {
    Console.WriteLine( e.Message );
   }
   finally
   {
    w.Stop();
   }
  }
  // Dump all properties
  public void CDREventArrived(object sender, EventArrivedEventArgs e)
  {
   // Get the Event object and display it
   PropertyData pd = e.NewEvent.Properties["TargetInstance"];
   if (pd != null)
   {
    ManagementBaseObject mbo = pd.Value as ManagementBaseObject;

    // if CD removed VolumeName == null
    if (mbo.Properties["VolumeName"].Value != null)
    {
     Console.WriteLine("CD has been inserted");
    }
    else
    {
     Console.WriteLine("CD has been ejected");
    }
   }
  }
 }
}

希望本文所述对大家的C#程序设计有所帮助。

(0)

相关推荐

  • C#利用win32 Api 修改本地系统时间、获取硬盘序列号

    C#利用win32 Api 修改本地系统时间.获取硬盘序列号,可以用于软件注册机制的编写! 复制代码 代码如下: using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace Fengyun {     public class Win32     {         #region 修改本地系统时间         [DllIm

  • c#判断磁盘驱动器类型的两种方法介绍

    1.使用WINDOWS API 复制代码 代码如下: /// <summary> /// 判断一个磁盘驱动器的类型 /// </summary> /// <param name="nDrive">包含了驱动器根目录路径的一个字串</param> /// <returns>Long,如驱动器不能识别,则返回零.如指定的目录不存在,则返回1.如执行成功,则用下述任何一个常数指定驱动器类型:DRIVE_REMOVABLE, DRIV

  • C#获得MAC地址(网卡序列号)的实现代码

    代码如下: 复制代码 代码如下: //获得网卡序列号    public string GetMoAddress()    {        string MoAddress = " ";        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");        ManagementObjectCollection moc2 = mc.GetInstanc

  • C#实现获取磁盘空间大小的方法

    本文实例讲述了C#实现获取磁盘空间大小的方法.分享给大家供大家参考.具体实现方法如下: 方法一:利用System.IO.DriveInfo.GetDrives方法来获取 复制代码 代码如下: ///   /// 获取指定驱动器的空间总大小(单位为B) ///   ///  只需输入代表驱动器的字母即可 (大写) ///    public static long GetHardDiskSpace(string str_HardDiskName) {     long totalSize= new

  • C# 键盘Enter键取代Tab键实现代码

    说明:在填写表数据时当输入完一个文本框后,输入下一个文本框时需要用Tab键切换,但是有的人喜欢用Enter键切换下一个,此方法是Enter取代Tab键. 效果: 注释:键盘Enter键数字为:13 键盘Tab键数字为:9 函数代码: 复制代码 代码如下: private void SetEnt()        {            foreach (Control txt in (this.Page.Form.FindControl("ContentPlaceHolderEntity&qu

  • C#中winform实现自动触发鼠标、键盘事件的方法

    程序触发鼠标.键盘事件是C#程序设计中比较常见的功能,本文实例展示了C#中winform实现自动触发鼠标.键盘事件的方法,有不错的实用价值.具体如下: 要想在C#程序中触发鼠标.键盘事件就必须要调用windows函数. 一.鼠标事件的触发 1.引用windows函数mouse_event /// <summary> /// 鼠标事件 /// </summary> /// <param name="flags">事件类型</param> /

  • C#获取硬盘编号的方法

    本文实例讲述了C#获取硬盘编号的方法.分享给大家供大家参考.具体实现方法如下: ManagementClass mc = new ManagementClass("Win32_PhysicalMedia"); //Win32_DiskDrive不包含SerialNumber属性. ManagementObjectCollection moc = mc.GetInstances(); string strID = null ; foreach( ManagementObject mo i

  • C#获取U盘序列号的方法

    本文实例讲述了C#获取U盘序列号的方法.分享给大家供大家参考.具体如下: using System.Management; private List<string> _serialNumber = new List<string>(); /// <summary> /// 调用这个函数将本机所有U盘序列号存储到_serialNumber中 /// </summary> private void matchDriveLetterWithSerial() { s

  • 用C#获取硬盘序列号,CPU序列号,网卡MAC地址的源码

    privatestring[]GetMoc() { string[]str=newstring[3]; ManagementClassmcCpu=newManagementClass("win32_Processor"); ManagementObjectCollectionmocCpu=mcCpu.GetInstances(); foreach(ManagementObjectminmocCpu) { str[0]=m["ProcessorId"].ToStrin

  • C#检测是否有u盘插入的方法

    本文实例讲述了C#检测是否有u盘插入的方法.分享给大家供大家参考.具体如下: 该C#代码可监控是否有u盘插入,同时可以监控其它驱动器的变化 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Ru

随机推荐