diff --git a/Vocaluxe/Base/CBackgroundMusic.cs b/Vocaluxe/Base/CBackgroundMusic.cs index 19c47c19..5050b408 100644 --- a/Vocaluxe/Base/CBackgroundMusic.cs +++ b/Vocaluxe/Base/CBackgroundMusic.cs @@ -157,7 +157,7 @@ namespace Vocaluxe.Base if (_Initialized) return; - List soundFiles = CHelper.ListSoundFiles(CSettings.FolderNameBackgroundMusic, true, true); + IEnumerable soundFiles = CHelper.ListSoundFiles(CSettings.FolderNameBackgroundMusic, true, true); foreach (string path in soundFiles) _BGMusicFiles.Add(new CPlaylistElement(path)); diff --git a/Vocaluxe/Base/CCover.cs b/Vocaluxe/Base/CCover.cs index 6dea5e2f..3ac5a87b 100644 --- a/Vocaluxe/Base/CCover.cs +++ b/Vocaluxe/Base/CCover.cs @@ -174,7 +174,7 @@ namespace Vocaluxe.Base _CoverThemes.Clear(); string folderPath = Path.Combine(CSettings.ProgramFolder, CSettings.FolderNameCover); - List files = CHelper.ListFiles(folderPath, "*.xml"); + IEnumerable files = CHelper.ListFiles(folderPath, "*.xml"); var xml = new CXmlDeserializer(); foreach (string file in files) @@ -207,7 +207,7 @@ namespace Vocaluxe.Base Debug.Assert(!String.IsNullOrEmpty(coverTheme.Info.Name)); - List files = CHelper.ListImageFiles(coverTheme.FolderPath, true, true); + IEnumerable files = CHelper.ListImageFiles(coverTheme.FolderPath, true, true); lock (_Covers) { diff --git a/Vocaluxe/Base/CSongs.cs b/Vocaluxe/Base/CSongs.cs index 555fe93e..3a442fea 100644 --- a/Vocaluxe/Base/CSongs.cs +++ b/Vocaluxe/Base/CSongs.cs @@ -348,8 +348,8 @@ namespace Vocaluxe.Base { if (Directory.Exists(path)) { - files = files.Union(Directory.EnumerateFiles(path, "*.txt", SearchOption.AllDirectories)); - files = files.Union(Directory.EnumerateFiles(path, "*.txd", SearchOption.AllDirectories)); + files = files.Union(CHelper.ListFiles(path, "*.txt", true, true)); + files = files.Union(CHelper.ListFiles(path, "*.txd", true, true)); } } } diff --git a/Vocaluxe/Base/ThemeSystem/CTheme.cs b/Vocaluxe/Base/ThemeSystem/CTheme.cs index 12ea5eaf..404517c0 100644 --- a/Vocaluxe/Base/ThemeSystem/CTheme.cs +++ b/Vocaluxe/Base/ThemeSystem/CTheme.cs @@ -86,7 +86,7 @@ namespace Vocaluxe.Base.ThemeSystem } string path = Path.Combine(_Folder, Name); - List files = CHelper.ListFiles(path, "*.xml"); + IEnumerable files = CHelper.ListFiles(path, "*.xml"); // Load skins, succeed if at least 1 skin was loaded bool ok = false; diff --git a/Vocaluxe/Base/ThemeSystem/CThemes.cs b/Vocaluxe/Base/ThemeSystem/CThemes.cs index 8ca65c47..1262c473 100644 --- a/Vocaluxe/Base/ThemeSystem/CThemes.cs +++ b/Vocaluxe/Base/ThemeSystem/CThemes.cs @@ -135,7 +135,7 @@ namespace Vocaluxe.Base.ThemeSystem public static bool ReadThemesFromFolder(string path, int partyModeID) { - List files = CHelper.ListFiles(path, "*.xml", false, true); + IEnumerable files = CHelper.ListFiles(path, "*.xml", false, true); List newThemes = new List(); foreach (string file in files) diff --git a/VocaluxeLib/CHelper.cs b/VocaluxeLib/CHelper.cs index 6c04fee7..20efe943 100644 --- a/VocaluxeLib/CHelper.cs +++ b/VocaluxeLib/CHelper.cs @@ -20,6 +20,7 @@ using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.IO; +using System.Linq; using VocaluxeLib.Log; namespace VocaluxeLib @@ -165,25 +166,20 @@ namespace VocaluxeLib /// Search directories recursively /// False for just file names, True for full path /// List of file names - public static List ListFiles(string path, string searchPattern, bool recursive = false, bool fullpath = false) + public static IEnumerable ListFiles(string path, string searchPattern, bool recursive = false, bool fullpath = false) { - var files = new List(); + IEnumerable files = Enumerable.Empty(); var dir = new DirectoryInfo(path); if (!dir.Exists) return files; + if (recursive && !fullpath) + throw new NotSupportedException("recursive file listing with relative path names is not supported"); + try { - // ReSharper disable LoopCanBeConvertedToQuery - foreach (FileInfo file in dir.GetFiles(searchPattern)) - // ReSharper restore LoopCanBeConvertedToQuery - files.Add(!fullpath ? file.Name : file.FullName); - - if (recursive) - { - foreach (DirectoryInfo di in dir.GetDirectories()) - files.AddRange(ListFiles(di.FullName, searchPattern, true, fullpath)); - } + files = Directory.EnumerateFiles(dir.FullName, searchPattern, recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly); + files = files.Select(fullpath ? (Func)Path.GetFullPath : Path.GetFileName); } catch (Exception) {} @@ -198,13 +194,9 @@ namespace VocaluxeLib /// Search directories recursively /// False for just file names, True for full path /// List of file names - public static List ListFiles(string path, IEnumerable searchPatterns, bool recursive = false, bool fullpath = false) + public static IEnumerable ListFiles(string path, IEnumerable searchPatterns, bool recursive = false, bool fullpath = false) { - var files = new List(); - foreach (string pattern in searchPatterns) - files.AddRange(ListFiles(path, pattern, recursive, fullpath)); - - return files; + return searchPatterns.SelectMany(pattern => ListFiles(path, pattern, recursive, fullpath)); } /// @@ -214,7 +206,7 @@ namespace VocaluxeLib /// Search directories recursively /// False for just file names, True for full path /// List of image file names - public static List ListImageFiles(string path, bool recursive = false, bool fullpath = false) + public static IEnumerable ListImageFiles(string path, bool recursive = false, bool fullpath = false) { return ListFiles(path, _ImageFileTypes, recursive, fullpath); } @@ -226,7 +218,7 @@ namespace VocaluxeLib /// Search directories recursively /// False for just file names, True for full path /// List of image file names - public static List ListSoundFiles(string path, bool recursive = false, bool fullpath = false) + public static IEnumerable ListSoundFiles(string path, bool recursive = false, bool fullpath = false) { return ListFiles(path, _SoundFileTypes, recursive, fullpath); } diff --git a/VocaluxeLib/Songs/CSong.cs b/VocaluxeLib/Songs/CSong.cs index fb9c033e..e6ddaf4d 100644 --- a/VocaluxeLib/Songs/CSong.cs +++ b/VocaluxeLib/Songs/CSong.cs @@ -325,7 +325,7 @@ namespace VocaluxeLib.Songs { if (CoverFileName == "") { - List files = CHelper.ListImageFiles(Folder); + IEnumerable files = CHelper.ListImageFiles(Folder); foreach (String file in files) { if (file.ContainsIgnoreCase("[CO]") && @@ -336,7 +336,7 @@ namespace VocaluxeLib.Songs if (BackgroundFileNames.Count == 0) { - List files = CHelper.ListImageFiles(Folder); + IEnumerable files = CHelper.ListImageFiles(Folder); foreach (String file in files) { if (file.ContainsIgnoreCase("[BG]") &&