Question

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[])?

Was it helpful?

Solution 2

Why not directly?

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

OTHER TIPS

Yeah or create a single-item array:

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

Use:

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top