【问题标题】:Loop through directories and search for .JPG filess遍历目录并搜索 .JPG 文件
【发布时间】:2010-10-15 15:52:46
【问题描述】:

我有这段代码,我得到一个 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);
                }

  }

【问题讨论】:

    标签: c# asp.net directory


    【解决方案1】:

    很抱歉第二个答案,但我想我看到了一个逻辑错误...

    我假设在每次迭代中,您都想在 当前文件夹 中搜索文件,并获取子目录,然后将它们传回函数(顺便说一下,递归的使用很好)并重复,直到没有更多的子目录。

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

    尝试更改此设置

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

    【讨论】:

    • 谢谢,这是问题之一。我将整个目录结构复制到我的硬盘驱动器,它工作正常。所以一定有你提到的权限问题。
    【解决方案2】:

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

    This Microsoft article 显示了一种可能的解决方案。

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

    编辑

    这里是:Asp.net Access To Network Share

    【讨论】:

    • 你知道我需要什么样的文件夹权限吗?这是一个共享点文件夹。我可以读取子目录,但不能读取主目录。
    猜你喜欢
    • 2021-10-24
    • 1970-01-01
    • 2014-10-01
    • 2020-11-05
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    相关资源
    最近更新 更多