Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Files.cs
1using System;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
6using Waher.Events;
7
8namespace Waher.Runtime.IO
9{
13 public static class Files
14 {
20 public static async Task<byte[]> ReadAllBytesAsync(string FileName)
21 {
22 using (FileStream fs = File.OpenRead(FileName))
23 {
24 return await fs.ReadAllAsync();
25 }
26 }
27
33 public static Task WriteAllBytesAsync(string FileName, byte[] Data)
34 {
35 return WriteAllBytesAsync(FileName, Data, 0, Data.Length);
36 }
37
45 public static async Task WriteAllBytesAsync(string FileName, byte[] Data, int Offset, int Length)
46 {
47 using (FileStream fs = File.Create(FileName))
48 {
49 await fs.WriteAsync(Data, Offset, Length);
50 }
51 }
52
58 public static Task AppendAllBytesAsync(string FileName, byte[] Data)
59 {
60 return AppendAllBytesAsync(FileName, Data, 0, Data.Length);
61 }
62
70 public static async Task AppendAllBytesAsync(string FileName, byte[] Data, int Offset, int Length)
71 {
72 using (FileStream fs = File.OpenWrite(FileName))
73 {
74 fs.Position = fs.Length;
75 await fs.WriteAsync(Data, Offset, Length);
76 }
77 }
78
84 public static async Task<string> ReadAllTextAsync(string FileName)
85 {
86 byte[] Bin = await ReadAllBytesAsync(FileName);
87 return Strings.GetString(Bin, Encoding.UTF8);
88 }
89
95 public static Task WriteAllTextAsync(string FileName, string Text)
96 {
97 return WriteAllTextAsync(FileName, Text, Encoding.UTF8);
98 }
99
106 public static async Task WriteAllTextAsync(string FileName, string Text, Encoding Encoding)
107 {
108 using (FileStream fs = File.Create(FileName))
109 {
110 byte[] Preamble = Encoding.GetPreamble();
111 byte[] Data = Encoding.GetBytes(Text);
112 int i, c = Preamble.Length;
113
114 if (c > 0)
115 {
116 if (c > Data.Length)
117 i = 0;
118 else
119 {
120 for (i = 0; i < c; i++)
121 {
122 if (Preamble[i] != Data[i])
123 break;
124 }
125 }
126
127 if (i < c)
128 await fs.WriteAsync(Preamble, 0, c);
129 }
130
131 await fs.WriteAsync(Data, 0, Data.Length);
132 }
133 }
134
140 public static void DeleteOldFiles(string FolderName, TimeSpan MaxAge)
141 {
142 DeleteOldFiles(FolderName, MaxAge, SearchOption.TopDirectoryOnly, false);
143 }
144
152 public static void DeleteOldFiles(string FolderName, TimeSpan MaxAge, SearchOption SearchOption, bool DeleteEmptySubFolders)
153 {
154 if (string.IsNullOrEmpty(FolderName))
155 FolderName = ".";
156
157 FolderName = Path.GetFullPath(FolderName);
158 if (FolderName[FolderName.Length - 1] != Path.DirectorySeparatorChar)
159 FolderName += Path.DirectorySeparatorChar;
160
161 string[] Files = Directory.GetFiles(FolderName, "*.*", SearchOption);
162 Dictionary<string, bool> Folders = new Dictionary<string, bool>();
163 double MaxDays = MaxAge.TotalDays;
164
165 foreach (string FileName in Files)
166 {
167 if ((DateTime.UtcNow - File.GetLastWriteTimeUtc(FileName)).TotalDays >= MaxDays)
168 {
169 try
170 {
171 File.Delete(FileName);
172 Folders[Path.GetDirectoryName(FileName)] = true;
173 }
174 catch (IOException ex)
175 {
176 Log.Error("Unable to delete file: " + ex.Message, FileName);
177 }
178 catch (Exception ex)
179 {
180 Log.Exception(ex);
181 }
182 }
183 }
184
185 if (DeleteEmptySubFolders && SearchOption != SearchOption.TopDirectoryOnly)
186 {
187 foreach (string Folder in Folders.Keys)
188 {
189 if (Folder.Length <= FolderName.Length)
190 continue;
191
192 try
193 {
194 IEnumerable<string> Entries = Directory.EnumerateFileSystemEntries(Folder);
195
196 using (IEnumerator<string> Enumerator = Entries.GetEnumerator())
197 {
198 if (Enumerator.MoveNext())
199 continue;
200
201 try
202 {
203 Directory.Delete(Folder);
204 }
205 catch (IOException ex)
206 {
207 Log.Error("Unable to delete folder: " + ex.Message, Folder);
208 }
209 }
210 }
211 catch (Exception ex)
212 {
213 Log.Exception(ex);
214 }
215 }
216 }
217 }
218 }
219}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
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.
Definition: Log.cs:1657
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.
Definition: Log.cs:692
Contains static methods
Definition: Files.cs:14
static async Task WriteAllBytesAsync(string FileName, byte[] Data, int Offset, int Length)
Creates a binary file asynchronously.
Definition: Files.cs:45
static Task WriteAllBytesAsync(string FileName, byte[] Data)
Creates a binary file asynchronously.
Definition: Files.cs:33
static async Task< string > ReadAllTextAsync(string FileName)
Reads a text file asynchronously.
Definition: Files.cs:84
static void DeleteOldFiles(string FolderName, TimeSpan MaxAge)
Deletes old files.
Definition: Files.cs:140
static async Task< byte[]> ReadAllBytesAsync(string FileName)
Reads a binary file asynchronously.
Definition: Files.cs:20
static Task AppendAllBytesAsync(string FileName, byte[] Data)
Appends a binary file asynchronously.
Definition: Files.cs:58
static async Task AppendAllBytesAsync(string FileName, byte[] Data, int Offset, int Length)
Appends a binary file asynchronously.
Definition: Files.cs:70
static void DeleteOldFiles(string FolderName, TimeSpan MaxAge, SearchOption SearchOption, bool DeleteEmptySubFolders)
Deletes old files.
Definition: Files.cs:152
static Task WriteAllTextAsync(string FileName, string Text)
Creates a text file asynchronously.
Definition: Files.cs:95
static async Task WriteAllTextAsync(string FileName, string Text, Encoding Encoding)
Creates a text file asynchronously.
Definition: Files.cs:106
Static class managing binary representations of strings.
Definition: Strings.cs:10
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.
Definition: Strings.cs:148