views:

46

answers:

2

I have this code and I'm getting an IOException and can't figure out what the problem is. I'm trying to loop through the subdirectories in a directory and list all of the .JPG files.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            Session["AllEmpsLoadPath"] = "\\\\intranet.org\\Photo Album\\Employees";

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        DirSearch((string)Session["AllEmpsLoadPath"]);

    }

 void DirSearch(string sDir) 
 {

                foreach (string d in Directory.GetDirectories(sDir)) 
                {

                    //I get an IOException here on the first iteration
                    //saying "There are no more files" and f is null
                    //even though there are subdirectories 
                        foreach (string f in Directory.GetFiles(d, "*.JPG"))
                        {
                            BulletedList1.Items.Add(f);
                        }

                    DirSearch(d);
                }

  }
+2  A: 

Most likely you need to correct a permissions issue. That's especially difficult when running under the normal ASP.NET User account, accessing a UNC share.

This Microsoft article shows one possible solution.

Personally, I would map drive in code . I've posted code for this here before. If I can find it I'll give you a link.

Edit

here it is: http://stackoverflow.com/questions/1435753/asp-net-access-to-network-share/1435789#1435789

David Stratton
If I pass in the subdirectory it works
Joshua Slocum
Do you know what kind of permission I would need on the folder? it's a share point folder. I can read the subdirectories, but not the main directory.
Joshua Slocum
A: 

Sorry about a second answer, but I think I see a logic error...

I'm assuming that on each iteration you want to search for files in the current folder, and get the subdirectories, then pass those back to the function (nice use of recursion by the way) and repeat until there are no more subdirectories.

The way you have it coded, the function looks for files in the child directories of the current directory and then recursively calls the function for child folders. This would mean that on the lowest level, there would be no child folders and you'd get an error there. It doesn't explain why the error is occurring on the first folder, though.

try changing this

void DirSearch(string sDir)  
 { 

            foreach (string d in Directory.GetDirectories(sDir))  
            { 

                //I get an IOException here on the first iteration 
                //saying "There are no more files" and f is null 
                //even though there are subdirectories  
                    foreach (string f in Directory.GetFiles(d, "*.JPG")) 
                    { 
                        BulletedList1.Items.Add(f); 
                    } 

                DirSearch(d); 
            } 
} 

to this

void DirSearch(string sDir)  
 { 
      foreach (string f in Directory.GetFiles(sDir, "*.JPG")) 
                        { 
                            BulletedList1.Items.Add(f); 
                        } 



                foreach (string d in Directory.GetDirectories(sDir))  
                { 

                    //I get an IOException here on the first iteration 
                    //saying "There are no more files" and f is null 
                    //even though there are subdirectories  
                                 DirSearch(d); 
                } 

  } 
David Stratton
thanks that was one of the problems. I copied to whole directory sturcture to my hard drive and it worked correctly. so there must be a permission issue as you mentioned.
Joshua Slocum