使用 DriveInfo 的本地驱动器的 LINQ 枚举
我知道有很多方法可以完成我在这里尝试做的事情,但我想用 LINQ 尝试一下,并且看到了一些奇怪的(对我来说)结果。希望这里有人能给我一些智慧。
目标是在本地计算机上找到我们使用已格式化为 NTFS 文件系统的最多可用可用空间执行操作的驱动器,如果可能,不包括 %SystemDrive% 卷。另一个需要注意的是,分配了卷号但尚未格式化的驱动器必须从候选池中排除。这是一种边缘情况,但一位客户已经遇到过这种情况。
所以,我正在尝试:
string systemDrive = Environment.GetEnvironmentVariable("SystemDrive") + "\\";
List<DriveInfo> localDrives = DriveInfo.GetDrives().ToList();
var bestAPIDataDrives = from localDrive in localDrives
orderby localDrive.AvailableFreeSpace descending
where localDrive.DriveType == DriveType.Fixed &&
localDrive.DriveFormat == "NTFS" &&
localDrive.RootDirectory.FullName != systemDrive &&
localDrive.IsReady
select localDrive.RootDirectory.FullName;
string path = String.Empty;
if (bestAPIDataDrives.Count() == 0)
{
// there is only a system drive available, use it anyway.
path = systemDrive;
}
else
{
path = bestAPIDataDrives.ElementAt(0).ToString();
}
当我到达 bestAPIDataDrives.Count() 参考时,我收到一个 IOException,其中 Message 属性设置为“设备尚未准备好”。
我不明白为什么会发生这种情况,因为我在 LINQ 查询中进行了 localDrive.IsReady 测试,或者至少我认为我这样做了。
I know there are many ways to accomplish what I am trying to do here, but I wanted to try it with LINQ, and am seeing some odd (to me) results. Hoping someone here has some wisdom to offer me.
Goal is to find the drive on the local machine where we execute with the most available free space that has been formatted to NTFS file system, excluding the %SystemDrive% volume if possible. Another caveat is that drives that are assigned a volume letter, but have not yet been formatted must be excluded from the candidate pool. This is an edge case, but one customer has already encountered it.
So, I am trying:
string systemDrive = Environment.GetEnvironmentVariable("SystemDrive") + "\\";
List<DriveInfo> localDrives = DriveInfo.GetDrives().ToList();
var bestAPIDataDrives = from localDrive in localDrives
orderby localDrive.AvailableFreeSpace descending
where localDrive.DriveType == DriveType.Fixed &&
localDrive.DriveFormat == "NTFS" &&
localDrive.RootDirectory.FullName != systemDrive &&
localDrive.IsReady
select localDrive.RootDirectory.FullName;
string path = String.Empty;
if (bestAPIDataDrives.Count() == 0)
{
// there is only a system drive available, use it anyway.
path = systemDrive;
}
else
{
path = bestAPIDataDrives.ElementAt(0).ToString();
}
When I get to the bestAPIDataDrives.Count() reference, I get an IOException with Message property set to "The device is not ready".
I don't see why this is happening because I have the localDrive.IsReady test in the LINQ query, or at least I think I do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是 LINQ-to-Objects,因此它将按照给出的顺序执行操作;特别是,它会在过滤之前尝试排序,并且几乎所有过滤器在关键的 IsReady 测试之前执行。我会简单地重新排序它们:
无论如何,在过滤器之后运行排序是一个好主意,因为您可以减少排序的成本(这总是超线性的)
正如克里斯指出的,然后您只想完成:
that is LINQ-to-Objects, so it will execute the operations in the order presented; in particular, it will try to sort before it has filtered, and almost all of the filters execute before the key
IsReady
test. I would simply re-order them:Running sorts after filters is a good idea anyway, as you reduce the cost of sorting (which is always super-linear)
As Chris notes, you then just want to finish up with: