0

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

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
Adsy2010
  • 525
  • 6
  • 23

4 Answers4

4

Yeah or create a single-item array:

FileInfo[] files = new FileInfo[] { info };
Robin Kanters
  • 5,018
  • 2
  • 20
  • 36
3

Why not directly?

paths.Select(p => new FileInfo(p)).ToArray();
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
2

Use:

var result = paths.Select(p => new FileInfo(p)).ToArray();
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
0

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();
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110