473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Detect CD-ROM

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
8 13576
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
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
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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: vega | last post by:
How do I detect empty tags if I have the DOM document? For example: <br /> and <br></br> I tried org.w3c.dom.Node.getFirstChild(), it returns null for both <br /> and <br></br> I also tried...
1
by: John Rauhe | last post by:
Hello, Does anybody know how to detect if an mass-storage device has been added to the system ? I am making a program that will (should) detect when a CompactFlash memory card has been inserted...
3
by: Flix | last post by:
I need to detect the root directories of the installed hard disks (es: C:, D:, E:, etc.). I'm not interested in cd drives. I know that there is a way (a bit slow, if I remeber) to retrive all the...
9
by: Paul Steele | last post by:
I am writing a C# app that needs to periodically poll for cdroms and usb storage device insertions. I've looked at the WMI functions but haven't found anything all that useful. The closest is...
4
by: Bmack500 | last post by:
I have the following bit of code: j2 = 0 If srvSC(j1) = "XCP CD Proxy" Then ReDim Preserve badGuys(j2) badGuys(j2) = j1 j2 += 1 End If Using .net methods, how can I detect if the badGuys...
4
by: Quina | last post by:
Hi, I'm trying to build an aplication to manage my discs (CD/DVD) and i'm trying also to add an automatic info colector. I can access the volume label, the size an even the date of some recorded...
3
by: ipellew | last post by:
Hi; I have a DBS that that generates some javasript and allows the user to alter table contents. If the database is being executed on a CD/DVD how can I tell is the current directory is...
2
by: Yisehaq | last post by:
hello Everyone I am working on a CD which contains data to be explored usingOLAP tools. (Pivot tables) I have made it an autorun and the pages comes and using pivot tables component one can...
2
by: arcade2084 | last post by:
I am trying to detect when a CD is inserted from a windows service. I have went through the route of creating a hiden window and trying to use the WndProc to detect the event; although the window...
1
by: ragini b | last post by:
Hello Is there any code in Java to detect CD ? Is there any Java package related to drivers,interfaces,hardware?
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.