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;
2using System.Collections.Generic;
3using System.IO;
4using System.Runtime.InteropServices;
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 }
160 }
161
162 foreach (string Path in AppendWith)
163 Result.Add(Path);
164
165 return Result.ToArray();
166 }
167
177 public static string FindLatestFile(Environment.SpecialFolder[] Folders, string Pattern, bool IncludeSubfolders)
178 {
179 return FindLatestFile(GetFolders(Folders), Pattern, IncludeSubfolders);
180 }
181
191 public static string FindLatestFile(string[] Folders, string Pattern, bool IncludeSubfolders)
192 {
193 return FindLatestFile(Folders, Pattern, IncludeSubfolders ? int.MaxValue : 0);
194 }
195
205 public static string FindLatestFile(string[] Folders, string Pattern, int SubfolderDepth)
206 {
207 string[] Files = FindFiles(Folders, Pattern, SubfolderDepth, int.MaxValue);
208 string Result = string.Empty;
209 DateTime BestTP = DateTime.MinValue;
210 DateTime TP;
211
212 foreach (string FilePath in Files)
213 {
214 TP = File.GetCreationTimeUtc(FilePath);
215 if (TP > BestTP)
216 {
217 BestTP = TP;
218 Result = FilePath;
219 }
220 }
221
222 return Result;
223 }
224
228 public static string ExecutableExtension
229 {
230 get
231 {
232 switch (Environment.OSVersion.Platform)
233 {
234 case PlatformID.Win32S:
235 case PlatformID.Win32Windows:
236 case PlatformID.Win32NT:
237 case PlatformID.WinCE:
238 return ".exe";
239
240 default:
241 return string.Empty;
242 }
243 }
244 }
245 }
246}
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:205
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:191
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:229
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:177