5using System.Threading.Tasks;
22 using (FileStream fs = File.OpenRead(FileName))
24 return await fs.ReadAllAsync();
45 public static async Task
WriteAllBytesAsync(
string FileName,
byte[] Data,
int Offset,
int Length)
47 using (FileStream fs = File.Create(FileName))
49 await fs.WriteAsync(Data, Offset, Length);
72 using (FileStream fs = File.OpenWrite(FileName))
74 fs.Position = fs.Length;
75 await fs.WriteAsync(Data, Offset, Length);
108 using (FileStream fs = File.Create(FileName))
110 byte[] Preamble = Encoding.GetPreamble();
111 byte[] Data = Encoding.GetBytes(Text);
112 int i, c = Preamble.Length;
120 for (i = 0; i < c; i++)
122 if (Preamble[i] != Data[i])
128 await fs.WriteAsync(Preamble, 0, c);
131 await fs.WriteAsync(Data, 0, Data.Length);
142 DeleteOldFiles(FolderName, MaxAge, SearchOption.TopDirectoryOnly,
false);
152 public static void DeleteOldFiles(
string FolderName, TimeSpan MaxAge, SearchOption SearchOption,
bool DeleteEmptySubFolders)
154 if (
string.IsNullOrEmpty(FolderName))
157 FolderName = Path.GetFullPath(FolderName);
158 if (FolderName[FolderName.Length - 1] != Path.DirectorySeparatorChar)
159 FolderName += Path.DirectorySeparatorChar;
161 string[]
Files = Directory.GetFiles(FolderName,
"*.*", SearchOption);
162 Dictionary<string, bool> Folders =
new Dictionary<string, bool>();
163 double MaxDays = MaxAge.TotalDays;
165 foreach (
string FileName
in Files)
167 if ((DateTime.UtcNow - File.GetLastWriteTimeUtc(FileName)).TotalDays >= MaxDays)
171 File.Delete(FileName);
172 Folders[Path.GetDirectoryName(FileName)] =
true;
174 catch (IOException ex)
176 Log.
Error(
"Unable to delete file: " + ex.Message, FileName);
185 if (DeleteEmptySubFolders && SearchOption != SearchOption.TopDirectoryOnly)
187 foreach (
string Folder
in Folders.Keys)
189 if (Folder.Length <= FolderName.Length)
194 IEnumerable<string> Entries = Directory.EnumerateFileSystemEntries(Folder);
196 using (IEnumerator<string> Enumerator = Entries.GetEnumerator())
198 if (Enumerator.MoveNext())
203 Directory.Delete(Folder);
205 catch (IOException ex)
207 Log.
Error(
"Unable to delete folder: " + ex.Message, Folder);
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
static void Error(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an error event.
static async Task WriteAllBytesAsync(string FileName, byte[] Data, int Offset, int Length)
Creates a binary file asynchronously.
static Task WriteAllBytesAsync(string FileName, byte[] Data)
Creates a binary file asynchronously.
static async Task< string > ReadAllTextAsync(string FileName)
Reads a text file asynchronously.
static void DeleteOldFiles(string FolderName, TimeSpan MaxAge)
Deletes old files.
static async Task< byte[]> ReadAllBytesAsync(string FileName)
Reads a binary file asynchronously.
static Task AppendAllBytesAsync(string FileName, byte[] Data)
Appends a binary file asynchronously.
static async Task AppendAllBytesAsync(string FileName, byte[] Data, int Offset, int Length)
Appends a binary file asynchronously.
static void DeleteOldFiles(string FolderName, TimeSpan MaxAge, SearchOption SearchOption, bool DeleteEmptySubFolders)
Deletes old files.
static Task WriteAllTextAsync(string FileName, string Text)
Creates a text file asynchronously.
static async Task WriteAllTextAsync(string FileName, string Text, Encoding Encoding)
Creates a text file asynchronously.
Static class managing binary representations of strings.
static string GetString(byte[] Data, int Offset, int Count, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.