简体   繁体   中英

How to convert FileInfo into FileInfo[]

I have been working on a program that requires a different approach to finish a job using try and catch nested within another try/catch .

For this purpose I have had to create a set of files as strings and then converted them to FileInfo .

IEnumerable<string> paths = null;
foreach (String fil in paths)
    FileInfo h = new FileInfo(fil);

So That wasn't so difficult, I require FileInfo to be in the form of a FileInfo[] array to continue the program however.

System.IO.FileInfo[] files = null;

Simply put, what is the best method to convert one type to the other, either directly from the string of from the converted FileInfo into a FileInfo array ( FileInfo[] )?

是的,或者创建一个单项数组:

FileInfo[] files = new FileInfo[] { info };

为什么不直接呢?

paths.Select(p => new FileInfo(p)).ToArray();

采用:

var result = paths.Select(p => new FileInfo(p)).ToArray();

You could just use Linq select to create your FileInfo[]

 IEnumerable<string> paths = null; // assuming you are going to fill this with filenames
 FileInfo[] fileInfos = paths.Select(p => new FileInfo(p)).ToArray();

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