1

I want to read files from the removable disk's(CD/DVD). Using below code i'm able to find the drive.

ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM    Win32_DiskDrive");

foreach (ManagementObject mo in mos.Get())
{
var Name=mo["Caption"].ToString();
var Drive=mo["Drive"].ToString();
var Id=mo["Id"].ToString();
}  

Now i want to read the files from the DVD.... Any help would be appreciated.. Thanks in Advance!!

1
  • Reading a file from a CD or DVD is no different than reading a file from the hard disk. Commented Oct 7, 2013 at 11:18

1 Answer 1

0

I'm not entirely sure what you mean when you say "read the files form the DVD", but you can use the following to get all of the files on each of your CD/DVD drives

static IEnumerable<string> getDirectoryFilePaths(string path)
{
    List<string> filePaths = new List<string>();

    try
    {
        // recursively look through all of the folders
        foreach (var dir in Directory.GetDirectories(path, "*"))
        {
            filePaths.AddRange(getDirectoryFilePaths(dir));
        }
    }
    catch (UnauthorizedAccessException)
    {
        // skip this stuff
    }

    // add the files directly in the current drive/folder
    filePaths.AddRange(Directory.GetFiles(path, "*").ToList());

    return filePaths;
}

static void Main(string[] args)
{
    // Get all of the ready CD drives
    foreach (var cdDrive in DriveInfo.GetDrives().Where(d => d.DriveType == DriveType.CDRom && d.IsReady))
    {
        // Start at the drive and get all of the files recursively
        IEnumerable<string> driveFiles = getDirectoryFilePaths(cdDrive.Name);

        foreach (var file in driveFiles)
        {
            // do something with the files...
            using (FileStream fs = File.OpenRead(file))
            {
                //...
            }
        }
    }
}

Obviously you can change the code to grab a specific drive rather than looking at all available, but hopefully this gets you going.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.