当您说“从 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))
{
//...
}
}
}
}
显然,您可以更改代码以获取特定驱动器,而不是查看所有可用驱动器,但希望这能让您继续前进。