简体   繁体   English

如何将DriveInfo []转换为列表 <string> C#

[英]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. 您好,我想使用loop将DriveInfo类型列表转换为字符串类型列表。在我的代码中,我尝试使用ToList(),但它不存在。 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. 使用select可以从选择的属性中使新的IEnumerable。 Using ToList() will convert to list. 使用ToList()将转换为列表。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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