我想从可移动磁盘(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中的文件.... 任何帮助,将不胜感激.. 在此先感谢!!
答案 0 :(得分: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))
{
//...
}
}
}
}
显然,您可以更改代码以获取特定的驱动器,而不是查看所有可用的驱动器,但希望这可以帮助您实现目标。