2

I am trying to retrieve all the files in all the folders I have in a directory .

But the result is quite random ..

I think the foreach is wrong ..

What I don't understand is why ?

Because in all the folders , we check all the files and then display a link buttons of all the files . But actually it's displaying a lot of folders , twice .

 var DI = new DirectoryInfo("C://inetpub//wwwroot//ClientPortal//Files//")
              .GetDirectories("*.*", System.IO.SearchOption.AllDirectories);

 foreach (System.IO.DirectoryInfo D1 in DI)
 {
     System.IO.FileInfo[] fiArr = D1.GetFiles();

     foreach (System.IO.FileInfo file in fiArr)
     {
         LinkButton lktest = new LinkButton();
         lktest.Text = D1.Name;
         form1.Controls.Add(lktest);
         form1.Controls.Add(new LiteralControl("<br>"));
     }
 }

Can someone help me ?

Thanks a lot !

1
  • This kind of thing should be pretty easy to debug assuming you are in a position to attach a debugger... Just run through step by step and every time you are adding a new control inspect things to work out whether you are getting the right value and if not what things are not as you expect... Commented Feb 13, 2012 at 17:04

3 Answers 3

5

display a link buttons of all the files

Here you're creating link buttons with the name set to the directory when it sounds like you want the file instead (ie file.Name instead of D1.Name)

lktest.Text = D1.Name;
Sign up to request clarification or add additional context in comments.

Comments

2

Does this help?

http://www.dreamincode.net/code/snippet1669.htm

public void GetDirStructure(string path)
{
    try
    {
        DirectoryInfo dir = new DirectoryInfo(path);
        DirectoryInfo[] subDirs = dir.GetDirectories();
        FileInfo[] files = dir.GetFiles();
        foreach(FileInfo fi in files)
        {
            Console.WriteLine(fi.FullName.ToString());
        }

        if (subDirs != null)
        {
            foreach (DirectoryInfo sd in subDirs)
            {
                GetDirStructure(path + @"\\" + sd.Name);
            }
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message.ToString());
    }
}

Comments

1

The first line of code seems like the culprit:

System.IO.DirectoryInfo[] DI = new System.IO.DirectoryInfo("C://inetpub//wwwroot//ClientPortal//Files//").GetDirectories("*.*", System.IO.SearchOption.AllDirectories);

Try using the following:

DirectoryInfo[] DI = new DirectoryInfo("C://inetpub//wwwroot//ClientPortal//File//").GetDirectories();

3 Comments

The point is to use the correct overload of the DirectoryInfo.GetDirectories() method.
But why is the one OP is using wrong and your suggestion right?
Because there is no need to specify search criteria. Have a look at the different method overloads here.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.