473,804 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13648
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

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*****@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

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(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


Nov 15 '05 #4
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



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.Manageme nt;
class App {
public static void Main() {
SelectQuery query = new SelectQuery("se lect volumename, volumeserialnum ber
from win32_logicaldi sk where drivetype=5");
ManagementObjec tSearcher searcher = new ManagementObjec tSearcher(query );
foreach (ManagementObje ct mo in searcher.Get()) {
// If both properties are null I suppose there's no CD
if((mo["volumename "] != null) || (mo["volumeserialnu mber"] != null))
Console.WriteLi ne("{0} - {1} ",mo["volumename "],
mo["volumeserialnu mber"]);
else
Console.WriteLi ne("No CD");

}
}
}

Willy.

"Hayato Iriumi" <hi*****@hotmai l.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.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 volumeserialnum ber in a service

TIA

John Hoffma

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

Try this

using System
using System.Manageme nt
class App
public static void Main()
SelectQuery query = new SelectQuery("se lect volumename, volumeserialnum be
from win32_logicaldi sk where drivetype=5")
ManagementObjec tSearcher searcher = new ManagementObjec tSearcher(query )
foreach (ManagementObje ct mo in searcher.Get())
// If both properties are null I suppose there's no C
if((mo["volumename "] != null) || (mo["volumeserialnu mber"] != null)
Console.WriteLi ne("{0} - {1} ",mo["volumename "]
mo["volumeserialnu mber"])
els
Console.WriteLi ne("No CD")

Willy

"Hayato Iriumi" <hi*****@hotmai l.com> wrote in messag
news:%2******** ********@TK2MSF TNGP11.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
2335
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 getNodeValue(), they both returns null also. I know <br /> and <br></br> are the same from the xml spec. Is there any way to tell the different syntax using DOM parser?
1
2943
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 into a PC (and is identified as a mass-storage device). However i cant fugure out how to do it using c#. regards john
3
11683
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 drives using a code like this: ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk"); ManagementObjectCollection disks = diskClass.GetInstances(); foreach (ManagementObject disk in disks) {
9
37212
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 Win32_DiskDrive, but it doesn't seem to return any information on cdrom devices. I suspect there might be a Win32 API call, but I haven't found any info yet using Google. Is there a way to achieve this in C#?
4
2402
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 array was never
4
5200
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 discs... but I can't determine whic kind of disc (CD/DVD) is on my drive. Can anybody help me? Thank you all in advance, João Carias
3
1740
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 unwritable from within the Access VB module? Regards Ian
2
2222
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 explore the data on it. My problem is if office or OWC is not installed on the PC it creates errors. Now, I want to detect where Pivot table or OWC is installed in advance and if not send a message to the user to intall it. I will put in one folder the...
2
3104
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 does not seem to be recieving the events that I need. I have also tried registering the WM_SHNOTIFY event with my specific window and still do not recieve the event. Is there another method for recieving the CD insert event from within a winows...
1
2135
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
9575
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10564
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10320
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.