2using System.Collections.Generic;
4using System.Runtime.InteropServices;
22 public static string[]
FindFiles(Environment.SpecialFolder[] Folders,
string Pattern,
bool IncludeSubfolders,
bool BreakOnFirst)
24 return FindFiles(
GetFolders(Folders), Pattern, IncludeSubfolders, BreakOnFirst ? 1 :
int.MaxValue);
36 public static string[]
FindFiles(
string[] Folders,
string Pattern,
bool IncludeSubfolders,
bool BreakOnFirst)
38 return FindFiles(Folders, Pattern, IncludeSubfolders, BreakOnFirst ? 1 :
int.MaxValue);
50 public static string[]
FindFiles(
string[] Folders,
string Pattern,
bool IncludeSubfolders,
int MaxCount)
52 return FindFiles(Folders, Pattern, IncludeSubfolders ?
int.MaxValue : 0, MaxCount);
64 public static string[]
FindFiles(
string[] Folders,
string Pattern,
int SubfolderDepth,
int MaxCount)
67 throw new ArgumentException(
"Must be positive.", nameof(MaxCount));
69 LinkedList<KeyValuePair<string, int>> ToProcess =
new LinkedList<KeyValuePair<string, int>>();
70 Dictionary<string, bool> Processed =
new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
71 List<string> Result =
new List<string>();
74 foreach (
string Folder
in Folders)
75 ToProcess.AddLast(
new KeyValuePair<string, int>(Folder, SubfolderDepth));
77 while (!(ToProcess.First is
null))
79 KeyValuePair<string, int> Processing = ToProcess.First.Value;
80 string Folder = Processing.Key;
81 int Depth = Processing.Value;
83 ToProcess.RemoveFirst();
84 if (Processed.ContainsKey(Folder))
87 if (!Directory.Exists(Folder))
90 Processed[Folder] =
true;
94 string[] Names = Directory.GetFiles(Folder, Pattern, SearchOption.TopDirectoryOnly);
96 foreach (
string FileName
in Names)
99 if (++Count >= MaxCount)
100 return Result.ToArray();
105 Names = Directory.GetDirectories(Folder);
107 foreach (
string SubFolder
in Names)
108 ToProcess.AddLast(
new KeyValuePair<string, int>(SubFolder, Depth));
117 return Result.ToArray();
126 public static string[]
GetFolders(Environment.SpecialFolder[] Folders, params
string[] AppendWith)
128 List<string> Result =
new List<string>();
130 foreach (Environment.SpecialFolder Folder in Folders)
132 string Path = Environment.GetFolderPath(Folder, Environment.SpecialFolderOption.None);
134 if (!
string.IsNullOrEmpty(Path))
138 if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
142 case Environment.SpecialFolder.CommonProgramFiles:
143 case Environment.SpecialFolder.ProgramFiles:
144 case Environment.SpecialFolder.System:
145 if (Path.EndsWith(
" (x86)"))
147 Path = Path.Substring(0, Path.Length - 6);
148 if (!Directory.Exists(Path))
157 if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && Path.StartsWith(
"/usr/share"))
158 Result.Add(Path.Replace(
"/usr/share",
"/usr/local/share"));
162 foreach (
string Path
in AppendWith)
165 return Result.ToArray();
177 public static string FindLatestFile(Environment.SpecialFolder[] Folders,
string Pattern,
bool IncludeSubfolders)
191 public static string FindLatestFile(
string[] Folders,
string Pattern,
bool IncludeSubfolders)
193 return FindLatestFile(Folders, Pattern, IncludeSubfolders ?
int.MaxValue : 0);
205 public static string FindLatestFile(
string[] Folders,
string Pattern,
int SubfolderDepth)
207 string[] Files =
FindFiles(Folders, Pattern, SubfolderDepth,
int.MaxValue);
208 string Result =
string.Empty;
209 DateTime BestTP = DateTime.MinValue;
212 foreach (
string FilePath
in Files)
214 TP = File.GetCreationTimeUtc(FilePath);
232 switch (Environment.OSVersion.Platform)
234 case PlatformID.Win32S:
235 case PlatformID.Win32Windows:
236 case PlatformID.Win32NT:
237 case PlatformID.WinCE:
Static class helping modules to find files installed on the system.
static string[] GetFolders(Environment.SpecialFolder[] Folders, params string[] AppendWith)
Gets the physical locations of special folders.
static string[] FindFiles(string[] Folders, string Pattern, bool IncludeSubfolders, bool BreakOnFirst)
Finds files in a set of folders, and optionally, their subfolders. This method only finds files in fo...
static string[] FindFiles(Environment.SpecialFolder[] Folders, string Pattern, bool IncludeSubfolders, bool BreakOnFirst)
Finds files in a set of folders, and optionally, their subfolders. This method only finds files in fo...
static string FindLatestFile(string[] Folders, string Pattern, int SubfolderDepth)
Finds the latest file matching a search pattern, by searching in a set of folders,...
static string FindLatestFile(string[] Folders, string Pattern, bool IncludeSubfolders)
Finds the latest file matching a search pattern, by searching in a set of folders,...
static string[] FindFiles(string[] Folders, string Pattern, int SubfolderDepth, int MaxCount)
Finds files in a set of folders, and optionally, their subfolders. This method only finds files in fo...
static string ExecutableExtension
Extension used by executable files on the platform.
static string[] FindFiles(string[] Folders, string Pattern, bool IncludeSubfolders, int MaxCount)
Finds files in a set of folders, and optionally, their subfolders. This method only finds files in fo...
static string FindLatestFile(Environment.SpecialFolder[] Folders, string Pattern, bool IncludeSubfolders)
Finds the latest file matching a search pattern, by searching in a set of folders,...