-3

I have a directory '0'(say) and subfolders and files as shown below

0-
 0.1
    0.1.1
         A.rdb  
         B.xml
     0.1.2
         A.rdb  
         B.xml
 0.2
    0.2.1
         A.rdb  
         B.xml
     0.1.2
         A.rdb  
         B.xml

here is the code that i am using for it is below,

List<string> folders = new List<string>();
        DirectoryInfo di = new DirectoryInfo(textBox1.Text);
        IEnumerable<string> IEstring;
        IEstring = from subdirectory in di.GetDirectories()
                   where subdirectory.GetFiles("*.*", SearchOption.AllDirectories).Length > 0
                   select subdirectory.Name;
        folders = IEstring.ToList();
        List<String> files = DirSearch(textBox1.Text);
        textBox2.Text = textBox2.Text+ displayMembers(files);

The problem is i am unable to print this list named "files" I don't know where I am doing wrong here.

I also needed the result of folders and files to be printed in new excel spreadsheet. The result show be the same as this

0-
     0.1
        0.1.1
             A.rdb  
             B.xml
         0.1.2
             A.rdb  
             B.xml
     0.2
        0.2.1
             A.rdb  
             B.xml
         0.1.2
             A.rdb  
             B.xml

But in spreadsheet.

Avenger
  • 53
  • 1
  • 6

2 Answers2

4

Method 1

HierarchicalItem holds the information we need about an item in the folder. An item can be a folder or file.

class HierarchicalItem
{
    public string Name;
    public int Deepth;

    public HierarchicalItem(string name, int deepth)
    {
        this.Name = name;
        this.Deepth = deepth;
    }
}

SearchDirectory is the recursive function to convert and flatten the tree structure to a list

    static IEnumerable<HierarchicalItem> SearchDirectory(DirectoryInfo directory, int deep = 0)
    {
        yield return new HierarchicalItem(directory.Name, deep);

        foreach (var subdirectory in directory.GetDirectories())
            foreach (var item in SearchDirectory(subdirectory , deep + 1))
                yield return item;

        foreach (var file in directory.GetFiles())
            yield return new HierarchicalItem(file.Name, deep + 1);
    }

Example :

        //fake TextBox...
        var textBox1 = new { Text = @"D:\Code\C#" };
        var directory = new DirectoryInfo(textBox1.Text);

        var items = SearchDirectory(directory);

        //you can print it to a console
        foreach (var item in items)
            Console.WriteLine(string.Concat(Enumerable.Repeat('\t', item.Deepth)) + item.Name);

        //or a textbox
        textBox1.Text = string.Join("\n", items.Select(i =>
            string.Concat(Enumerable.Repeat('\t', i.Deepth)) + i.Name));

        //or a table
        for (int i = 0; i < items.Count(); i++)
        {
            //with row = i, and column = items[i].Deepth + 1
        }

Method 2

This recursive method prints the directory like the tree command in CommandLine

    static string ScanFolder(DirectoryInfo directory, string indentation = "\t", int maxLevel = -1, int deep = 0)
    {
        StringBuilder builder = new StringBuilder();

        builder.AppendLine(string.Concat(Enumerable.Repeat(indentation, deep)) + directory.Name);

        if (maxLevel == -1 || deep < maxLevel )
        {
            foreach (var subdirectory in directory.GetDirectories())
                builder.Append(ScanFolder(subdirectory, indentation, maxLevel, deep + 1));
        }

        foreach (var file in directory.GetFiles())
            builder.AppendLine(string.Concat(Enumerable.Repeat(indentation, deep + 1)) + file.Name);

        return builder.ToString();
    }
Burhan
  • 668
  • 5
  • 27
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
0

Does this get you closer?

Func<DirectoryInfo, int, IEnumerable<string>> fetch = null;
fetch =
    (di, l) =>
        new [] { "".PadLeft(l * 4) + di.Name }
            .Concat(di.GetDirectories().SelectMany(di2 => fetch(di2, l + 1)))
            .Concat(di.GetFiles().Select(
                fi => "".PadLeft((l + 1) * 4) + fi.Name));

Please note that the null assignment is necessary as this Func is recursive.

You would call it like this:

var results =
    fetch(new DirectoryInfo(@"C:\Top Folder"), 0)
        .ToList();
Enigmativity
  • 113,464
  • 11
  • 89
  • 172