Here's how you can do it using System.Manageme nt classes (and WMI).
// This code demonstrates how to monitor the CDROM device loading
using System;
using System.Componen tModel;
using System.Runtime. InteropServices ;
using System.Manageme nt;
class WMIEvent {
public static void Main() {
WMIEvent we = new WMIEvent();
ManagementEvent Watcher w= null;
WqlEventQuery q;
ManagementOpera tionObserver observer = new ManagementOpera tionObserver();
// Bind to local machine
ManagementScope scope = new ManagementScope ("root\\CIMV2") ;
scope.Options.E nablePrivileges = true; //sets required privilege
try {
q = new WqlEventQuery() ;
q.EventClassNam e = "__InstanceModi ficationEvent";
q.WithinInterva l = new TimeSpan(0,0,15 );
// DriveType - 5: CDROM
q.Condition = @"TargetInstanc e ISA 'Win32_LogicalD isk' and
TargetInstance. DriveType = 5";
Console.WriteLi ne(q.QueryStrin g);
w = new ManagementEvent Watcher(scope, q);
// register async. event handler
w.EventArrived += new EventArrivedEve ntHandler(we.CD REventArrived);
w.Start();
// Do something usefull,block thread for testing
Console.ReadLin e();
}
catch(Exception e) {
Console.WriteLi ne(e.Message);
}
finally {
w.Stop();
}
}
// Dump all properties
public void CDREventArrived (object sender, EventArrivedEve ntArgs e) {
//Get the Event object and display it
PropertyData pd;
if(( pd = e.NewEvent.Prop erties["TargetInstance "]) != null)
{
ManagementBaseO bject mbo = pd.Value as ManagementBaseO bject;
// if CD removed VolumeName == null
if(mbo.Properti es["VolumeName "].Value != null)
Console.WriteLi ne(mbo.Properti es["VolumeName "].Value);
}
}
}
Willy.
"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:eq******** ******@TK2MSFTN GP10.phx.gbl...
Intersting problem, not sure about how to determine when a disc is
inserted, but I am working on a lower level piece of code(which will be written in
MC++) that will query cdrom media properties(writ eability, etc) which
should be done in a day or so. If no one posts an answer I can post the resultant
code when its complete.
"Tony" <La*********@ho meandresting.co m> wrote in message
news:Ot******** ******@TK2MSFTN GP11.phx.gbl... I cant help on this, but am looking for similar information.... ..
I am trying to do something similar, but would also like to know if the
cd drive is writeable and has writeable media loaded.
The same goes for a floppy drive - seeing if a disk is loaded and the
disk is not write protected
Tony
"Hayato Iriumi" <hi*****@hotmai l.com> wrote in message
news:e1******** *******@TK2MSFT NGP11.phx.gbl.. . Hello,
I'm trying to find a way to detect whether CD-ROM was inserted into
the CD-ROM drive using C#. Does anyone have any idea how to do this? WMI?
Win32 API?
TIA