Read on CDrom

  • Thread starter Thread starter bac
  • Start date Start date
B

bac

Dear every member,

First thanks for reading this post.

Could you suggust me some ideas how to read file struct (all file
names/folders) on on CDrom?

Thanks again

See your reply soon
 
Bac,

Reading file and directory informtion from a cd-rom is no different
than reading from a harddrive. You should look at the FileInfo and
DirectoryInfo classes found in the System.IO namespace. You could
use a recursive method to read the entier contents. Please let me know
if you need some sample code for this.

Also, do you know which device your cd-rom is, or do you need to
find this out as well (i.e do you know the drive-letter you want to read
from or should this be done automatically by code as well?)

HTH,

//Andreas
 
Bac,

I will split the source code into two replies to make it more readable.
In this
reply I will show you how you can get the drives and the type of each drive
on
your system. You will need to use some p/Invoke to call the GetDriveType()
API. For this we create a class called Win32 to place the call and an enum.

public class Win32
{
private Win32()
{
}

public enum DriveType : uint
{
Unknown = 0,
NoRootDir = 1,
Removable = 2,
Fixed = 3,
Remote = 4,
CdRom = 5,
RamDisk = 6
}

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern DriveType GetDriveType(
string rootPathName);
}

Now we have everything we need from Win32, and we can create an application
(in my case a console application) to try it out.

string[] drives = Environment.GetLogicalDrives();
foreach(string drive in drives)
{
Console.WriteLine(drive);
switch(Win32.GetDriveType(drive))
{
case Win32.DriveType.CdRom:
Console.WriteLine("CdRom");
break;
case Win32.DriveType.Fixed:
Console.WriteLine("Fixed");
break;
case Win32.DriveType.RamDisk:
Console.WriteLine("RamDisk");
break;
case Win32.DriveType.Remote:
Console.WriteLine("Remote");
break;
case Win32.DriveType.Removable:
Console.WriteLine("Removable");
break;
}
}
Console.ReadLine();

Please refer to my other reply to see how you can scan all of the folders on
a drive to get all the files.

HTH,

//Andreas
 
Bac,

In this reply I will show you how to scan each folder on a drive and get
the
file information for all the files. You will have to use the FileInfo and
DirectoryInfo
classes in the System.IO namespace.

public class DriveScanner
{
private DriveScanner()
{
}
public static void Scan(string drive)
{
DirectoryInfo dirInfo =
new DirectoryInfo(drive);
DriveScanner.Scan(dirInfo);
}

public static void Scan(DirectoryInfo dirInfo)
{
Console.WriteLine("Entering directory {0}", dirInfo.FullName);
FileInfo[] files = dirInfo.GetFiles();

foreach(FileInfo file in files)
Console.WriteLine(file.Name);

foreach(DirectoryInfo info in dirInfo.GetDirectories())
DriveScanner.Scan(info);
}
}

Call this class using DriveScanner.Scan("c:\") etc. In your example you
would
send the variable "drive" to the Scan() method when the switch is
Win32.DriverType.CdRom

HTH,

//Andreas
 
Thanks for you reply,

I will look more in MSDN, but it's better if you could send an example.

My application is used to store the CD content (files name only) into
database, so I need to detect and read CDrom.

Thanks again.

--------------------------------------
 
Back
Top