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

Identifying a CD Drive

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
"=?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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.