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

3
  • A FileInfo[] array is precisely: FileInfo[][]. i.e. not what you want.
    – Cole Tobin
    Dec 22, 2012 at 23:10
  • Also. If paths is null, how will you create an array?
    – Cole Tobin
    Dec 22, 2012 at 23:11
  • Paths is not null, that's just the setup line
    – Adsy2010
    Dec 23, 2012 at 9:19

4 Answers 4

4

Yeah or create a single-item array:

FileInfo[] files = new FileInfo[] { info };
1
  • 1
    The method I am using is a sub method requiring the addition to anything already in the array
    – Adsy2010
    Dec 23, 2012 at 9:22
3

Why not directly?

paths.Select(p => new FileInfo(p)).ToArray();
1
  • @Adsy2010: Note that you need at least .NET 3.5 for using System.Linq. Dec 23, 2012 at 10:01
2

Use:

var result = paths.Select(p => new FileInfo(p)).ToArray();
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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.