By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

Detect CD-ROM

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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*****@hotmail.com> wrote in message
news:e1***************@TK2MSFTNGP11.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

Nov 15 '05 #2

P: n/a
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*****@hotmail.com> wrote in message
news:e1***************@TK2MSFTNGP11.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

Nov 15 '05 #3

P: n/a
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(writeability, 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*********@homeandresting.com> wrote in message
news:Ot**************@TK2MSFTNGP11.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*****@hotmail.com> wrote in message
news:e1***************@TK2MSFTNGP11.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


Nov 15 '05 #4

P: n/a
Here's how you can do it using System.Management classes (and WMI).

// This code demonstrates how to monitor the CDROM device loading
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Management;
class WMIEvent {
public static void Main() {
WMIEvent we = new WMIEvent();
ManagementEventWatcher w= null;
WqlEventQuery q;
ManagementOperationObserver observer = new ManagementOperationObserver();
// Bind to local machine
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true; //sets required privilege
try {
q = new WqlEventQuery();
q.EventClassName = "__InstanceModificationEvent";
q.WithinInterval = new TimeSpan(0,0,15);
// DriveType - 5: CDROM
q.Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and
TargetInstance.DriveType = 5";
Console.WriteLine(q.QueryString);
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;
if(( pd = e.NewEvent.Properties["TargetInstance"]) != null)
{
ManagementBaseObject mbo = pd.Value as ManagementBaseObject;
// if CD removed VolumeName == null
if(mbo.Properties["VolumeName"].Value != null)
Console.WriteLine(mbo.Properties["VolumeName"].Value);
}
}
}

Willy.

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:eq**************@TK2MSFTNGP10.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(writeability, 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*********@homeandresting.com> wrote in message
news:Ot**************@TK2MSFTNGP11.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*****@hotmail.com> wrote in message
news:e1***************@TK2MSFTNGP11.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



Nov 15 '05 #5

P: n/a

This is great, but it only works when user inserts or ejects the CD-ROM?
Is there any way to check whether the CD-ROM is already in CD Drive or
not?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #6

P: n/a
Try this:

using System;
using System.Management;
class App {
public static void Main() {
SelectQuery query = new SelectQuery("select volumename, volumeserialnumber
from win32_logicaldisk where drivetype=5");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get()) {
// If both properties are null I suppose there's no CD
if((mo["volumename"] != null) || (mo["volumeserialnumber"] != null))
Console.WriteLine("{0} - {1} ",mo["volumename"],
mo["volumeserialnumber"]);
else
Console.WriteLine("No CD");

}
}
}

Willy.

"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...

This is great, but it only works when user inserts or ejects the CD-ROM?
Is there any way to check whether the CD-ROM is already in CD Drive or
not?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #7

P: n/a
Hello, Willy.
Wow, you're amazing. Thank you very much for the code. I really
appreciate it. :-)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #8

P: n/a
Any idea why this code won't run in a service? Runs well in a console app but comes up with null volumename and volumeserialnumber in a service

TIA

John Hoffma

----- Willy Denoyette [MVP] wrote: ----

Try this

using System
using System.Management
class App
public static void Main()
SelectQuery query = new SelectQuery("select volumename, volumeserialnumbe
from win32_logicaldisk where drivetype=5")
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query)
foreach (ManagementObject mo in searcher.Get())
// If both properties are null I suppose there's no C
if((mo["volumename"] != null) || (mo["volumeserialnumber"] != null)
Console.WriteLine("{0} - {1} ",mo["volumename"]
mo["volumeserialnumber"])
els
Console.WriteLine("No CD")

Willy

"Hayato Iriumi" <hi*****@hotmail.com> wrote in messag
news:%2****************@TK2MSFTNGP11.phx.gbl..
This is great, but it only works when user inserts or ejects the CD-ROM

Is there any way to check whether the CD-ROM is already in CD Drive o
not
*** Sent via Developersdex http://www.developersdex.com **

Don't just participate in USENET...get rewarded for it


Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.