Directory.GetDirectories doesn't work as I thought

342 Views Asked by At
public Dir(string rootDir)
    {
        Directories = new List<string>();
        RootDir = rootDir;
        foreach(string dir in Directory.GetDirectories(RootDir, @"*.mp3", SearchOption.AllDirectories))
        {
            Directories.Add(dir);
            Debug.Print(dir);
        }
        Shuffle(Directories);
    }

With this code, I wanted to find all the .mp3 files in one folder, but it came out to make a zero element in 'Directories'. What seems to be the problem?

1

There are 1 best solutions below

0
J-Roel On BEST ANSWER

*.mp3 are files. Use Directory.GetFiles();

If you are looking for all mp3s within folders. Do something like this (pseudo-code):

List<string> mp3s = new List<string>();
foreach(string directory in Directory.GetDirectories(_rootFolder)){
    foreach(string file in Directory.GetFiles(directory)){
        mp3s.Add(file);
    }
}