473,395 Members | 1,412 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,395 software developers and data experts.

Identifying a CD Drive

Is there a way to programmatically identify a local CD drive/drives from a
local machine. Currently, I am calling
System.IO.Directory.GetLogicalDrives() - but this lists all the drives on the
local machine. What is the best way to identify the CD drive(s)?
Nov 16 '05 #1
4 3468
"=?Utf-8?B?R3JlZ1Q=?=" <Gr***@discussions.microsoft.com> wrote in
news:9F**********************************@microsof t.com:
Is there a way to programmatically identify a local CD
drive/drives from a local machine. Currently, I am calling
System.IO.Directory.GetLogicalDrives() - but this lists all the
drives on the local machine. What is the best way to identify
the CD drive(s)?


Greg,

You can use the Windows API function GetDriveType:

// GetDriveType return values.
public enum DriveType : int
{
Unknown = 0,
NoRoot = 1,
Removable = 2,
Localdisk = 3,
Network = 4,
CD = 5,
RAMDrive = 6
}

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetDriveType(string lpRootPathName);
// Example:

if (GetDriveType("E:") == (int) DriveType.CD)
...
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #2
Hi Greg

You can use WMI, here is a sample that checks to see if a CD-ROM is in any
CD drive.

-- Begin Code --
using System;
using System.Management;

class App
{
public static void Main()
{
SelectQuery query = new SelectQuery( "select * 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( "CD is named: {0}", mo["volumename"] );
Console.WriteLine( "CD Serial Number: {0}",
mo["volumeserialnumber"] );
}
else
{
Console.WriteLine( "No CD in Unit" );
}
}

// Here to stop app from closing
Console.WriteLine( "\nPress Return to exit." );
Console.Read();
}
}
--End Code--

Hope this helps

Publicjoe

C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Tutorial at http://www.publicjoe.f9.co.uk/vbnet/vbnet.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html
Mirrors at http://www.dowhileloop.com/publicjoe/ and
http://www.publicjoe.justbe.com/

Useful Articles at http://www.madsally.co.uk

"GregT" <Gr***@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
Is there a way to programmatically identify a local CD drive/drives from a
local machine. Currently, I am calling
System.IO.Directory.GetLogicalDrives() - but this lists all the drives on the local machine. What is the best way to identify the CD drive(s)?

Nov 16 '05 #3
Thanks Chris,

I was leaning this way but I wanted to see if there was a .NET function l
could use to accomplish the same thing. I appreciate your input - enjoyed
your web page too, good insights.

Greg

"Chris R. Timmons" wrote:
"=?Utf-8?B?R3JlZ1Q=?=" <Gr***@discussions.microsoft.com> wrote in
news:9F**********************************@microsof t.com:
Is there a way to programmatically identify a local CD
drive/drives from a local machine. Currently, I am calling
System.IO.Directory.GetLogicalDrives() - but this lists all the
drives on the local machine. What is the best way to identify
the CD drive(s)?


Greg,

You can use the Windows API function GetDriveType:

// GetDriveType return values.
public enum DriveType : int
{
Unknown = 0,
NoRoot = 1,
Removable = 2,
Localdisk = 3,
Network = 4,
CD = 5,
RAMDrive = 6
}

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetDriveType(string lpRootPathName);
// Example:

if (GetDriveType("E:") == (int) DriveType.CD)
...
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Nov 16 '05 #4
Thanks! I'll have to look more into WMI as I've never used it before.

Greg

"Publicjoe" wrote:
Hi Greg

You can use WMI, here is a sample that checks to see if a CD-ROM is in any
CD drive.

-- Begin Code --
using System;
using System.Management;

class App
{
public static void Main()
{
SelectQuery query = new SelectQuery( "select * 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( "CD is named: {0}", mo["volumename"] );
Console.WriteLine( "CD Serial Number: {0}",
mo["volumeserialnumber"] );
}
else
{
Console.WriteLine( "No CD in Unit" );
}
}

// Here to stop app from closing
Console.WriteLine( "\nPress Return to exit." );
Console.Read();
}
}
--End Code--

Hope this helps

Publicjoe

C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Tutorial at http://www.publicjoe.f9.co.uk/vbnet/vbnet.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html
Mirrors at http://www.dowhileloop.com/publicjoe/ and
http://www.publicjoe.justbe.com/

Useful Articles at http://www.madsally.co.uk

"GregT" <Gr***@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
Is there a way to programmatically identify a local CD drive/drives from a
local machine. Currently, I am calling
System.IO.Directory.GetLogicalDrives() - but this lists all the drives on

the
local machine. What is the best way to identify the CD drive(s)?


Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David | last post by:
I know that identifying the user IP address with HTTP_SERVER_VARS; is reliant on the browser agent but I have stumpled upon the following code which I have tried to understand but failed! ...
7
by: GregT | last post by:
I program in C#. Is there a way to determine which of the local machine's logical drives are associated with CD drive(s)?
7
by: bill.brennum | last post by:
Hi, Have a number of Access Databases that I inherited and want to zip a few of them. My concern is that other active Microsoft Applications may be linking to the database or its tables. Is...
18
by: Jeff S | last post by:
It seems that anyone can put anything on their resume and refer to themselves as a "senior Web developer." What are some clues that can be used by an IT manager during a hiring process to...
2
by: Kavitha | last post by:
Hi , Can any one tel me how to create a virtual drive in C#( similar to Gmail Virtual drive).Also tell me what interfaces could be used to create the same. Thanks in Advance Kavitha
4
by: Luc The Perverse | last post by:
Hi - I have very little C# programming experience. I am making a software product which calls for an interface almost identical to Windows Explorer - and I wondered if mounting a "virtual drive"...
10
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under...
23
by: Rotsey | last post by:
Hi, I am writing an app that scans hard drives and logs info about every fine on the drive. The first iteration of my code used a class and a generic list to store the data and rhis took...
3
by: Paul Brady | last post by:
I have two users, Andrew and Nancy, who each have their own computer and can connect to a common database called "employees.mdb" which resides on a network server. On that same network server,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...

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.