0

是否可以将类型字符串列表转换为 DeviceInfo[]。我正在获取计算机上的逻辑驱动器列表并将其转换为列表以删除我的系统目录(我的操作系统目录)。现在我想将该列表转换回 DeviceInfo[] 因为我需要获取具有更多可用空间的逻辑驱动器。

DriveInfo[] drive = DriveInfo.GetDrives();
List<string> list = drive.Select(x => x.RootDirectory.FullName).ToList();
list.Remove(Path.GetPathRoot(Environment.SystemDirectory).ToString());

谢谢你。

4

3 回答 3

2

你不必做Select()

DriveInfo[] driveFiltered = drive.Where(x => x.RootDirectory.FullName != Path.GetPathRoot(Environment.SystemDirectory).ToString()).ToArray();

编辑:

正如@MarkFeldman 指出的那样,Path.GetPathRoot()DriveInfo[]. 这不会对这种特殊情况产生影响(除非您有几十个硬盘驱动器),但它可能会给您带来不良的 LINQ 习惯 :)。有效的方法是:

string systemDirectory = Path.GetPathRoot(Environment.SystemDirectory).ToString();
DriveInfo[] driveFiltered = drive.Where(x => x.RootDirectory.FullName != systemDirectory).ToArray();
于 2016-02-23T06:21:42.367 回答
0

为什么不直接使用这样的东西?

List<DriveInfo> list = DriveInfo.GetDrives().Where(x => x.RootDirectory.FullName != Path.GetPathRoot(Environment.SystemDirectory).ToString()).ToList();

这将避免转换为字符串列表,并保留原始 DriveInfo[] 数组的类型。

于 2016-02-23T06:35:22.233 回答
0

下面的代码将显示最大的可用空间;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication11
{
    class Program
    {

        static void Main(string[] args)
        {
            long FreeSize = 0;
            DriveInfo[] drive = DriveInfo.GetDrives().Where(x =>
            {
                if (x.RootDirectory.FullName != Path.GetPathRoot(Environment.SystemDirectory).ToString() && x.AvailableFreeSpace >= FreeSize)
                {
                    FreeSize = x.AvailableFreeSpace; 
                    Console.WriteLine("{0}Size:{1}", x.Name, x.AvailableFreeSpace);
                    return true;
                }
                else
                {
                    return false;
                }
            }).ToArray();

            Console.ReadLine();

        }
    }
}

截图 1

于 2016-02-23T07:08:11.170 回答