Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FileSystem.cs
1using System;
3using System.IO;
5
7{
11 public static class FileSystem
12 {
22 public static string[] FindFiles(Environment.SpecialFolder[] Folders, string Pattern, bool IncludeSubfolders, bool BreakOnFirst)
23 {
24 return FindFiles(GetFolders(Folders), Pattern, IncludeSubfolders, BreakOnFirst ? 1 : int.MaxValue);
25 }
26
36 public static string[] FindFiles(string[] Folders, string Pattern, bool IncludeSubfolders, bool BreakOnFirst)
37 {
38 return FindFiles(Folders, Pattern, IncludeSubfolders, BreakOnFirst ? 1 : int.MaxValue);
39 }
40
50 public static string[] FindFiles(string[] Folders, string Pattern, bool IncludeSubfolders, int MaxCount)
51 {
52 return FindFiles(Folders, Pattern, IncludeSubfolders ? int.MaxValue : 0, MaxCount);
53 }
54
64 public static string[] FindFiles(string[] Folders, string Pattern, int SubfolderDepth, int MaxCount)
65 {
66 if (MaxCount <= 0)
67 throw new ArgumentException("Must be positive.", nameof(MaxCount));
68
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>();
72 int Count = 0;
73
74 foreach (string Folder in Folders)
75 ToProcess.AddLast(new KeyValuePair<string, int>(Folder, SubfolderDepth));
76
77 while (!(ToProcess.First is null))
78 {
79 KeyValuePair<string, int> Processing = ToProcess.First.Value;
80 string Folder = Processing.Key;
81 int Depth = Processing.Value;
82
83 ToProcess.RemoveFirst();
84 if (Processed.ContainsKey(Folder))
85 continue;
86
87 if (!Directory.Exists(Folder))
88 continue;
89
90 Processed[Folder] = true;
91
92 try
93 {
94 string[] Names = Directory.GetFiles(Folder, Pattern, SearchOption.TopDirectoryOnly);
95
96 foreach (string FileName in Names)
97 {
98 Result.Add(FileName);
99 if (++Count >= MaxCount)
100 return Result.ToArray();
101 }
102
103 if (Depth-- > 0)
104 {
105 Names = Directory.GetDirectories(Folder);
106
107 foreach (string SubFolder in Names)
108 ToProcess.AddLast(new KeyValuePair<string, int>(SubFolder, Depth));
109 }
110 }
111 catch (Exception)
112 {
113 // Ignore
114 }
115 }
116
117 return Result.ToArray();
118 }
119
126 public static string[] GetFolders(Environment.SpecialFolder[] Folders, params string[] AppendWith)
127 {
128 List<string> Result = new List<string>();
129
130 foreach (Environment.SpecialFolder Folder in Folders)
131 {
132 string Path = Environment.GetFolderPath(Folder, Environment.SpecialFolderOption.None);
133
134 if (!string.IsNullOrEmpty(Path))
135 {
136 // In 64-bit operating systems, the 32-bit folder can be returned anyway, if the process is running in 32 bit.
137
138 if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
139 {
140 switch (Folder)
141 {
142 case Environment.SpecialFolder.CommonProgramFiles:
143 case Environment.SpecialFolder.ProgramFiles:
144 case Environment.SpecialFolder.System:
145 if (Path.EndsWith(" (x86)"))
146 {
147 Path = Path.Substring(0, Path.Length - 6);
148 if (!Directory.Exists(Path))
149 continue;
150 }
151 break;
152 }
153 }
154
155 Result.Add(Path);
156
157 if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && Path.StartsWith("/usr/share"))
158 Result.Add(Path.Replace("/usr/share", "/usr/local/share"));
159 else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && Path.StartsWith("/usr/share"))
160 Result.Add(Path.Replace("/usr/share", "/var/lib"));
161 }
162 }
163
164 foreach (string Path in AppendWith)
165 Result.Add(Path);
166
167 return Result.ToArray();
168 }
169
179 public static string FindLatestFile(Environment.SpecialFolder[] Folders, string Pattern, bool IncludeSubfolders)
180 {
181 return FindLatestFile(GetFolders(Folders), Pattern, IncludeSubfolders);
182 }
183
193 public static string FindLatestFile(string[] Folders, string Pattern, bool IncludeSubfolders)
194 {
195 return FindLatestFile(Folders, Pattern, IncludeSubfolders ? int.MaxValue : 0);
196 }
197
207 public static string FindLatestFile(string[] Folders, string Pattern, int SubfolderDepth)
208 {
209 string[] Files = FindFiles(Folders, Pattern, SubfolderDepth, int.MaxValue);
210 string Result = string.Empty;
211 DateTime BestTP = DateTime.MinValue;
212 DateTime TP;
213
214 foreach (string FilePath in Files)
215 {
216 TP = File.GetCreationTimeUtc(FilePath);
217 if (TP > BestTP)
218 {
219 BestTP = TP;
220 Result = FilePath;
221 }
222 }
223
224 return Result;
225 }
226
230 public static string ExecutableExtension
231 {
232 get
233 {
234 switch (Environment.OSVersion.Platform)
235 {
236 case PlatformID.Win32S:
237 case PlatformID.Win32Windows:
238 case PlatformID.Win32NT:
239 case PlatformID.WinCE:
240 return ".exe";
241
242 default:
243 return string.Empty;
244 }
245 }
246 }
247 }
248}
Static class helping modules to find files installed on the system.
Definition: FileSystem.cs:12
static string[] GetFolders(Environment.SpecialFolder[] Folders, params string[] AppendWith)
Gets the physical locations of special folders.
Definition: FileSystem.cs:126
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...
Definition: FileSystem.cs:36
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...
Definition: FileSystem.cs:22
static string FindLatestFile(string[] Folders, string Pattern, int SubfolderDepth)
Finds the latest file matching a search pattern, by searching in a set of folders,...
Definition: FileSystem.cs:207
static string FindLatestFile(string[] Folders, string Pattern, bool IncludeSubfolders)
Finds the latest file matching a search pattern, by searching in a set of folders,...
Definition: FileSystem.cs:193
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...
Definition: FileSystem.cs:64
static string ExecutableExtension
Extension used by executable files on the platform.
Definition: FileSystem.cs:231
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...
Definition: FileSystem.cs:50
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,...
Definition: FileSystem.cs:179