Is it possible to cast list of type string to DeviceInfo[]. I am fetching list of logical drives on my computer and casting it to list to remove my system directory(My Operating System directory). Now I want to cast that list back to DeviceInfo[] as I need to get the logical drive which has more space available.
DriveInfo[] drive = DriveInfo.GetDrives();
List<string> list = drive.Select(x => x.RootDirectory.FullName).ToList();
list.Remove(Path.GetPathRoot(Environment.SystemDirectory).ToString());
Thank You.
You don't have to do Select()
DriveInfo[] driveFiltered = drive.Where(x => x.RootDirectory.FullName != Path.GetPathRoot(Environment.SystemDirectory).ToString()).ToArray();
EDIT:
As @MarkFeldman pointed out, Path.GetPathRoot()
gets evaluated for all of the items on DriveInfo[]
. This won't make a difference for this particular case (unless you have like dozens of hard drives) but it might give you a bad LINQ habit :). The efficient way would be:
string systemDirectory = Path.GetPathRoot(Environment.SystemDirectory).ToString();
DriveInfo[] driveFiltered = drive.Where(x => x.RootDirectory.FullName != systemDirectory).ToArray();