简体   繁体   中英

How to convert DriveInfo[] to List<string> C#

hi I want to convert DriveInfo type list to string type list with using loop.In my code I am trying to use ToList() but it's not exist. actually I want all the paths of logical drives in string list without using loop. I know manually it's possible to use loop but I want to do this with direct function. Thank.

here's my code

DriveInfo[] Drive_info = DriveInfo.GetDrives();

List<string> list = Drive_info.ToList<string>();

Try this code:

DriveInfo[] Drive_info = DriveInfo.GetDrives();

List<string> list = Drive_info.Select(x => x.RootDirectory.FullName).ToList();

If I'm not wrong it takes path to all drives. Using select you can make new IEnumerable from property you choose. Using ToList() will convert to list.

List<string> list = Drive_info.Select (d => d.Name).ToList()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM