1

我想从可移动磁盘(CD/DVD)中读取文件。使用下面的代码我能够找到驱动器。

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();
}  

现在我想从 DVD 中读取文件......任何帮助将不胜感激......在此先感谢!

4

1 回答 1

0

当您说“从 DVD 读取文件”时,我不完全确定您的意思,但您可以使用以下命令获取每个 CD/DVD 驱动器上的所有文件

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))
            {
                //...
            }
        }
    }
}

显然,您可以更改代码以获取特定驱动器,而不是查看所有可用驱动器,但希望这能让您继续前进。

于 2014-07-02T00:01:12.980 回答