简体   繁体   English

如何获取目录和子目录的列表C#WPF

[英]How to get a list of directories and sub directories C# WPF

How would you get a list of all sub directories and their sub directories (until there are none left) from a root directory. 您将如何从根目录中获得所有子目录及其子目录(直到没有子目录)的列表。

My current code uses a FolderBrowserDialog (WPF does not have one so I have to take it from winforms), and from there attempts to transverse directories. 我当前的代码使用FolderBrowserDialog(WPF没有它,所以我必须从winforms中获取它),然后从那里尝试遍历目录。 So far I can only make it transverse 1 level deep, as in 到目前为止,我只能使它横向于1级深,例如

If the music directory is setup as 如果音乐目录设置为

\rock\metallica\ride the lightning
\rock\aerosmith\
\rock\something\

and the user chooses \\rock\\ it would only grab files from aerosmith, something, and metallica, and not the sub directory of metallica, ride the lightning. 而用户选择\\ rock \\,则只会从aerosmith,某物和metallica而不是metallica的子目录中获取文件,而是绕行闪电。

private void btnAddTestFile_Click(object sender, RoutedEventArgs e)
{
    string[] files;
    FolderBrowserDialog dlg = new FolderBrowserDialog();

    DialogResult result = dlg.ShowDialog();
    if (result.ToString() == "OK")
    {
        InitializePropertyValues();


        files = System.IO.Directory.GetFiles(dlg.SelectedPath, "*.mp3");
        foreach (string file in files)
        {
            lstvTrackList.Items.Add(new TrackList{ ID = lstvTrackList.Items.Count.ToString(), TrackName = Regex.Match(file, @".*\\(.*).mp3").Groups[1].Value, TrackPath = file });
        }

        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dlg.SelectedPath);
        foreach (System.IO.DirectoryInfo d in di.GetDirectories())
        {
            files = System.IO.Directory.GetFiles(d.FullName, "*.mp3");
            foreach (string file in files)
            {
                lstvTrackList.Items.Add(new TrackList{ ID = lstvTrackList.Items.Count.ToString(), TrackName = Regex.Match(file, @".*\\(.*).mp3").Groups[1].Value, TrackPath = file });
            }
        }
    }
}

Basically what I have attempted to do was first add all the loose files from the root directory, then get a list of directories inside the root directory and add those files. 基本上,我试图做的是首先从根目录中添加所有松散文件,然后在根目录中获取目录列表并添加这些文件。

I think I could figure it out if I knew how to crawl all sub directories starting from a root directory, but I don't quite understand how I would achieve that. 如果我知道如何从根目录开始爬网所有子目录,我想我可以弄清楚,但是我不太了解如何实现。

Does anyone have any hints, tips, or sample code that allows you to specify a root directory, then crawl every directory (keeping a string array) until no more sub directories are found, so I can grab files from each directory? 没有人有任何提示,技巧或示例代码,可让您指定根目录,然后对每个目录进行爬网(保留字符串数组),直到找不到更多子目录为止,这样我就可以从每个目录中抓取文件了?

To get all the sub directories you have to pass the SearchOption as the third argument, Which will return all the sub-directories as well. 要获取所有子目录,您必须将SearchOption作为第三个参数传递,该参数还将返回所有子目录。

Directory.GetFiles("","",SearchOption.AllDirectories);

SearchOption

  • SearchOption.AllDirectories : Includes the current directory and all its subdirectories in a search operation. SearchOption.AllDirectories :在搜索操作中包括当前目录及其所有子目录。 This option includes reparse points such as mounted drives and symbolic links in the search. 此选项包括重新分析点,例如搜索中的安装驱动器和符号链接。
  • SearchOption.TopDirectoryOnly : Includes only the current directory in a search operation. SearchOption.TopDirectoryOnly :在搜索操作中仅包括当前目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM