@Gonger96 das funktioniert auch, dauert aber gefühlt länger:
C#-Quellcode
- public static List<DirectoryItem> GetFilesFolders(this string path)
- {
- var items = new List<DirectoryItem>();
- if (!Directory.Exists(path)) return new List<DirectoryItem>();
- var folders = new DirectoryInfo(path).EnumerateDirectories();
- List<FileInfo> files = new DirectoryInfo(path).GetMP3Files();
- Parallel.ForEach(folders, (folder) =>
- {
- if (folder.HasMP3Files())
- items.Add(new DirectoryItem { FullPath = folder.FullName, Type = DirectoryItemTypeEnum.Folder });
- });
- if (files.Count > 0)
- {
- items.AddRange(files.Select(f => new DirectoryItem { FullPath = f.FullName, Type = DirectoryItemTypeEnum.File }));
- }
- return items;
- }
- #endregion
- public static bool HasMP3Files(this DirectoryInfo dirInfo)
- {
- try
- {
- return dirInfo.EnumerateFiles("*.mp3", SearchOption.AllDirectories).Any();
- }
- catch (UnauthorizedAccessException)
- {
- return false;
- }
- }
- public static List<FileInfo> GetMP3Files(this DirectoryInfo dirInfo)
- {
- if (!dirInfo.Exists)
- throw new DirectoryNotFoundException();
- try
- {
- return dirInfo.EnumerateFiles("*.mp3").ToList();
- }
- catch (Exception)
- {
- return null;
- }
- }
"Hier könnte Ihre Werbung stehen..."