2

I am wondering how can I return the list of my folders with the files inside.

Because I can't return both of the list...and can't put both types in the list.

    public List<DirectoryInfo> GetTopFolders()
    {
        List<DirectoryInfo> Folders = new List<DirectoryInfo>();
        List<FileInfo> Files = new List<FileInfo>();

        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("C://inetpub//wwwroot//Files//");
        di.GetDirectories();
        System.IO.FileInfo[] fiArr = di.GetFiles();

        foreach (DirectoryInfo t in di.GetDirectories())
        {
            Folders.Add(t);
            foreach (FileInfo f in di.GetFiles())
            {
                Files.Add(f);
            }
        }
        return Folders;
    }

This is the code I am actually using.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
Kiwimoisi
  • 4,086
  • 6
  • 33
  • 64
  • You can't return two different types. If directory and folder paths/names are enough, you can add them to a `List` and return that. – Oded Apr 12 '12 at 13:23
  • You could separate the functions. One without parameters that return the `Folders` and a second one with your `Folders` list as parameter that returns the `Files` – Loci Apr 12 '12 at 13:25
  • 1
    `FileInfo` has `FullName`, `Name` and `Directory` properties - seems this is everything you need. – Renatas M. Apr 12 '12 at 13:26

3 Answers3

5

Well, both FileInfo and DirectoryInfo inherit from FileSystemInfo. So you could maintain a list of that type instead and return the whole lot together:

public List<FileSystemInfo> GetTopFoldersAndFiles()
{
    List<FileSystemInfo> FilesAndFolders = new List<FileSystemInfo>();

    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("C://inetpub//wwwroot//Files//");
    di.GetDirectories();
    System.IO.FileInfo[] fiArr = di.GetFiles();

    foreach (DirectoryInfo t in di.GetDirectories())
    {
        FilesAndFolders.Add(t);
        foreach (FileInfo f in di.GetFiles())
        {
            FilesAndFolders.Add(f);
        }
    }
    return FilesAndFolders;
}

Of course you might need some conditional handling in the code where you make use of this object based on each item's type.

Sir Crispalot
  • 4,792
  • 1
  • 39
  • 64
  • +1 this is infact a more elegant solution! :) To get the specific class' properties he will have to cast though. – gideon Apr 12 '12 at 13:31
3

You could use a two-tuple. See Tuple.Create on msdn

public Tuple<List<DirectoryInfo>,List<FileInfo>> GetTopFolders()
{
    List<DirectoryInfo> Folders = new List<DirectoryInfo>();
    List<FileInfo> Files = new List<FileInfo>();

     /* All your other code here */
     /* Removed for brevity*/
    foreach (DirectoryInfo t in di.GetDirectories())
    {
        Folders.Add(t);
        foreach (FileInfo f in di.GetFiles())
        {
            Files.Add(f);
        }
    }
    return Tuple.Create(Folders, Files);
}

You use it like this:

var fileAndFolders = GetTopFolders()
fileAndFolders.Item1 //will be your folders of type List<DirectoryInfo>
fileAndFolders.Item2 //will be your folders of type List<FileInfo>
gideon
  • 19,329
  • 11
  • 72
  • 113
  • @Oded not sure I see what would go wrong. My answer is to his generic question _"How do I return two types from a function"_ – gideon Apr 12 '12 at 13:30
1

You can use a Dictionary<DirectoryInfo, List<FileInfo>>:

private Dictionary<DirectoryInfo, List<FileInfo>> GetTopFolders()
{
    Dictionary<DirectoryInfo, List<FileInfo>> r = new Dictionary<DirectoryInfo, List<FileInfo>>();


    DirectoryInfo di = new DirectoryInfo("C://inetpub//wwwroot//Files//"); 
    di.GetDirectories();

    r.Add(di, di.GetFiles().ToList());

    foreach (DirectoryInfo t in di.GetDirectories()) 
    {
        r.Add(t, t.GetFiles().ToList());
    } 

    return r; 
}

then you can list your folders and files like this:

StringBuilder sb = new StringBuilder();

foreach (KeyValuePair<DirectoryInfo, List<FileInfo>> item in GetTopFolders())
{
    sb.AppendLine(item.Key.FullName);

    foreach (FileInfo file in item.Value)
    {
        sb.AppendLine("\t" + file.Name);
    }
}

Console.WriteLine(sb.ToString());
Salvador Sarpi
  • 981
  • 1
  • 8
  • 19