I’m trying to improve my program which currently builds a generic list of strings as follows:
List<string> fileList = new List<string>(Directory.GetFiles(root, "*.xml", SearchOption.AllDirectories));
to a different generic list which is hopefully more flexible:
List<InputFileInfo> fileList = List<InputFileInfo>(Directory.GetFiles(root, "*.xml", SearchOption.AllDirectories));
public class InputFileInfo
{
public string _fullPath { get; set; }
public string _Message { get; set; }
public int _RowCount { get; set; }
public InputFileInfo(string fName)
{
_fullPath = fName;
_RowCount = 0;
_Message = String.Empty;
}
}
Directory.GetFiles returns a string array and I guess my InputFileInfo constructor is not sufficient. Do I need to loop thru a string array and populate List using an .Add method for each element of .GetFiles array or is there a way to populate my new List in one statement like the List of strings?
Yes – ideally with LINQ to Objects:
If you haven’t come across LINQ yet, I strongly recommend that you look at it in detail. It’s ideal for this sort of thing.