Identifying a CD Drive

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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)?
 
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)
...
 
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
 
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 said:
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)
...
 
Thanks! I'll have to look more into WMI as I've never used it before.

Greg

Publicjoe said:
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 said:
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)?
 
Back
Top