C#递归文件夹和文件目录

发布于 2025-01-05 18:04:18 字数 706 浏览 0 评论 0原文

我正在尝试检索目录中所有文件夹中的所有文件。

但结果是相当随机的..

我认为 foreach 是错误的..

我不明白的是为什么?

因为在所有文件夹中,我们检查了所有文件,然后显示所有文件的链接按钮。但实际上它显示了很多文件夹,两次。

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

有人可以帮助我吗?

多谢 !

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 !

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

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

发布评论

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

评论(3

甜点 2025-01-12 18:04:18

显示所有文件的链接按钮

在这里,您正在创建名称设置为目录的链接按钮,而听起来您想要的是文件(即 file.Name 而不是 D1.Name)

lktest.Text = D1.Name;

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;
找回味觉 2025-01-12 18:04:18

这有帮助吗?

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

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());
    }
}
一梦等七年七年为一梦 2025-01-12 18:04:18

第一行代码似乎是罪魁祸首:

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

尝试使用以下内容:

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

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