循环遍历目录并搜索 .JPG 文件

发布于 2024-09-27 07:30:17 字数 994 浏览 0 评论 0原文

我有这段代码,但收到 IOException 并且无法找出问题所在。 我试图循环遍历目录中的子目录并列出所有 .JPG 文件。

    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);
                }

  }

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);
                }

  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回梦里 2024-10-04 07:30:18

您很可能需要纠正权限问题。当在正常的 ASP.NET 用户帐户下运行并访问 UNC 共享时,这尤其困难。

这篇 Microsoft 文章展示了一种可能的解决方案。

就我个人而言,我会在代码中映射驱动器。我之前已经在这里发布过代码。如果我能找到它,我会给你一个链接。

编辑

在这里:Asp.net Access到网络共享

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: Asp.net Access To Network Share

掐死时间 2024-10-04 07:30:17

对第二个答案感到抱歉,但我认为我看到了逻辑错误...

我假设在每次迭代中您想要在当前文件夹中搜索文件,并获取子目录,然后通过那些回到函数(顺便说一句,很好地使用了递归)并重复,直到没有更多的子目录。

按照您的编码方式,该函数在当前目录的子目录中查找文件,然后递归调用子文件夹的函数。这意味着在最低级别上,不会有子文件夹,并且您会在那里收到错误。但它没有解释为什么第一个文件夹上发生错误。

尝试将其更改

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); 
            } 
} 

为此

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); 
                } 

  } 

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); 
                } 

  } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文