使用OOB应用程序。
dynamic fileSystem = AutomationFactory.CreateObject("Scripting.FileSystemObject");
dynamic drives = fileSystem.Drives;
foreach (var drive in drives)
{
DriveCollection.Add(new Drive{
VolumeName = drive.VolumeName,
DriveLetter = drive.DriveLetter,
TotalSpace = drive.TotalSize,
FreeSpace = drive.FreeSpace
});
listBox.Items.Add(string.Format("{0} {1}", drive.DriveLetter, drive.VolumeName));
}
在调试器中运行时,枚举'C'驱动器,然后抛出异常:
“HRESULT异常:0x800A0047”
当我从桌面运行应用程序时,在抛出异常之前不会枚举任何驱动器。
有关如何防止此异常的任何提示?以某种方式检查'drive'对象的有效性,然后在Drive构造函数中引用它?