-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathFileSystem.cs
More file actions
248 lines (218 loc) · 9.08 KB
/
FileSystem.cs
File metadata and controls
248 lines (218 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
namespace Waher.Content.SystemFiles
{
/// <summary>
/// Static class helping modules to find files installed on the system.
/// </summary>
public static class FileSystem
{
/// <summary>
/// Finds files in a set of folders, and optionally, their subfolders. This method only finds files in folders
/// the application as access rights to.
/// </summary>
/// <param name="Folders">Folders to search in.</param>
/// <param name="Pattern">Pattern to search for.</param>
/// <param name="IncludeSubfolders">If subfolders are to be included.</param>
/// <param name="BreakOnFirst">If the search should break when it finds the first file.</param>
/// <returns>Files matching the pattern found in the corresponding folders.</returns>
public static string[] FindFiles(Environment.SpecialFolder[] Folders, string Pattern, bool IncludeSubfolders, bool BreakOnFirst)
{
return FindFiles(GetFolders(Folders), Pattern, IncludeSubfolders, BreakOnFirst ? 1 : int.MaxValue);
}
/// <summary>
/// Finds files in a set of folders, and optionally, their subfolders. This method only finds files in folders
/// the application as access rights to.
/// </summary>
/// <param name="Folders">Folders to search in.</param>
/// <param name="Pattern">Pattern to search for.</param>
/// <param name="IncludeSubfolders">If subfolders are to be included.</param>
/// <param name="BreakOnFirst">If the search should break when it finds the first file.</param>
/// <returns>Files matching the pattern found in the corresponding folders.</returns>
public static string[] FindFiles(string[] Folders, string Pattern, bool IncludeSubfolders, bool BreakOnFirst)
{
return FindFiles(Folders, Pattern, IncludeSubfolders, BreakOnFirst ? 1 : int.MaxValue);
}
/// <summary>
/// Finds files in a set of folders, and optionally, their subfolders. This method only finds files in folders
/// the application as access rights to.
/// </summary>
/// <param name="Folders">Folders to search in.</param>
/// <param name="Pattern">Pattern to search for.</param>
/// <param name="IncludeSubfolders">If subfolders are to be included.</param>
/// <param name="MaxCount">Maximum number of files to return.</param>
/// <returns>Files matching the pattern found in the corresponding folders.</returns>
public static string[] FindFiles(string[] Folders, string Pattern, bool IncludeSubfolders, int MaxCount)
{
return FindFiles(Folders, Pattern, IncludeSubfolders ? int.MaxValue : 0, MaxCount);
}
/// <summary>
/// Finds files in a set of folders, and optionally, their subfolders. This method only finds files in folders
/// the application as access rights to.
/// </summary>
/// <param name="Folders">Folders to search in.</param>
/// <param name="Pattern">Pattern to search for.</param>
/// <param name="SubfolderDepth">Maximum folder depth to search.</param>
/// <param name="MaxCount">Maximum number of files to return.</param>
/// <returns>Files matching the pattern found in the corresponding folders.</returns>
public static string[] FindFiles(string[] Folders, string Pattern, int SubfolderDepth, int MaxCount)
{
if (MaxCount <= 0)
throw new ArgumentException("Must be positive.", nameof(MaxCount));
LinkedList<KeyValuePair<string, int>> ToProcess = new LinkedList<KeyValuePair<string, int>>();
Dictionary<string, bool> Processed = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
List<string> Result = new List<string>();
int Count = 0;
foreach (string Folder in Folders)
ToProcess.AddLast(new KeyValuePair<string, int>(Folder, SubfolderDepth));
while (!(ToProcess.First is null))
{
KeyValuePair<string, int> Processing = ToProcess.First.Value;
string Folder = Processing.Key;
int Depth = Processing.Value;
ToProcess.RemoveFirst();
if (Processed.ContainsKey(Folder))
continue;
if (!Directory.Exists(Folder))
continue;
Processed[Folder] = true;
try
{
string[] Names = Directory.GetFiles(Folder, Pattern, SearchOption.TopDirectoryOnly);
foreach (string FileName in Names)
{
Result.Add(FileName);
if (++Count >= MaxCount)
return Result.ToArray();
}
if (Depth-- > 0)
{
Names = Directory.GetDirectories(Folder);
foreach (string SubFolder in Names)
ToProcess.AddLast(new KeyValuePair<string, int>(SubFolder, Depth));
}
}
catch (Exception)
{
// Ignore
}
}
return Result.ToArray();
}
/// <summary>
/// Gets the physical locations of special folders.
/// </summary>
/// <param name="Folders">Special folders.</param>
/// <param name="AppendWith">Append result with this array of folders.</param>
/// <returns>Physical locations. Only the physical locations of defined special folders are returned.</returns>
public static string[] GetFolders(Environment.SpecialFolder[] Folders, params string[] AppendWith)
{
List<string> Result = new List<string>();
foreach (Environment.SpecialFolder Folder in Folders)
{
string Path = Environment.GetFolderPath(Folder, Environment.SpecialFolderOption.None);
if (!string.IsNullOrEmpty(Path))
{
// In 64-bit operating systems, the 32-bit folder can be returned anyway, if the process is running in 32 bit.
if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
{
switch (Folder)
{
case Environment.SpecialFolder.CommonProgramFiles:
case Environment.SpecialFolder.ProgramFiles:
case Environment.SpecialFolder.System:
if (Path.EndsWith(" (x86)"))
{
Path = Path.Substring(0, Path.Length - 6);
if (!Directory.Exists(Path))
continue;
}
break;
}
}
Result.Add(Path);
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && Path.StartsWith("/usr/share"))
Result.Add(Path.Replace("/usr/share", "/usr/local/share"));
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && Path.StartsWith("/usr/share"))
Result.Add(Path.Replace("/usr/share", "/var/lib"));
}
}
foreach (string Path in AppendWith)
Result.Add(Path);
return Result.ToArray();
}
/// <summary>
/// Finds the latest file matching a search pattern, by searching in a set of folders, and optionally, their subfolders.
/// This method only finds files in folders the application as access rights to. If no file is found, the empty string
/// is returned.
/// </summary>
/// <param name="Folders">Folders to search in.</param>
/// <param name="Pattern">Pattern to search for.</param>
/// <param name="IncludeSubfolders">If subfolders are to be included.</param>
/// <returns>Latest file if found, empty string if not.</returns>
public static string FindLatestFile(Environment.SpecialFolder[] Folders, string Pattern, bool IncludeSubfolders)
{
return FindLatestFile(GetFolders(Folders), Pattern, IncludeSubfolders);
}
/// <summary>
/// Finds the latest file matching a search pattern, by searching in a set of folders, and optionally, their subfolders.
/// This method only finds files in folders the application as access rights to. If no file is found, the empty string
/// is returned.
/// </summary>
/// <param name="Folders">Folders to search in.</param>
/// <param name="Pattern">Pattern to search for.</param>
/// <param name="IncludeSubfolders">If subfolders are to be included.</param>
/// <returns>Latest file if found, empty string if not.</returns>
public static string FindLatestFile(string[] Folders, string Pattern, bool IncludeSubfolders)
{
return FindLatestFile(Folders, Pattern, IncludeSubfolders ? int.MaxValue : 0);
}
/// <summary>
/// Finds the latest file matching a search pattern, by searching in a set of folders, and optionally, their subfolders.
/// This method only finds files in folders the application as access rights to. If no file is found, the empty string
/// is returned.
/// </summary>
/// <param name="Folders">Folders to search in.</param>
/// <param name="Pattern">Pattern to search for.</param>
/// <param name="SubfolderDepth">Maximum folder depth to search.</param>
/// <returns>Latest file if found, empty string if not.</returns>
public static string FindLatestFile(string[] Folders, string Pattern, int SubfolderDepth)
{
string[] Files = FindFiles(Folders, Pattern, SubfolderDepth, int.MaxValue);
string Result = string.Empty;
DateTime BestTP = DateTime.MinValue;
DateTime TP;
foreach (string FilePath in Files)
{
TP = File.GetCreationTimeUtc(FilePath);
if (TP > BestTP)
{
BestTP = TP;
Result = FilePath;
}
}
return Result;
}
/// <summary>
/// Extension used by executable files on the platform.
/// </summary>
public static string ExecutableExtension
{
get
{
switch (Environment.OSVersion.Platform)
{
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.Win32NT:
case PlatformID.WinCE:
return ".exe";
default:
return string.Empty;
}
}
}
}
}