-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFileUtils.cs
345 lines (296 loc) · 10.7 KB
/
FileUtils.cs
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
RMLib: Nonvisual support classes used by multiple R&M Software programs
Copyright (C) Rick Parrish, R&M Software
This file is part of RMLib.
RMLib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
RMLib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with RMLib. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace RandM.RMLib
{
public static class FileUtils
{
public delegate void ProgressCallback(long position, long fileSize, out bool abort);
// TODO Implement as event
public static ProgressCallback OnProgress = null;
private static void DoProgress(long position, long fileSize, out bool abort)
{
abort = false;
if (OnProgress != null) OnProgress(position, fileSize, out abort);
}
/// <summary>
/// DirectoryDelete is an exception ignoring wrapper for System.IO.Directory.Delete()
/// </summary>
/// <param name="directoryName">The name of the directory to delete</param>
public static void DirectoryDelete(string directoryName)
{
try
{
// Try to remove the directory
Directory.Delete(directoryName);
}
catch (Exception)
{
// This function ignores errors by design
}
}
public static void FileAppendAllText(string fileName, string text)
{
FileAppendAllText(fileName, text, Encoding.Default);
}
public static void FileAppendAllText(string fileName, string text, Encoding encoding)
{
IOException LastException = null;
for (int i = 0; i < 5; i++)
{
try
{
File.AppendAllText(fileName, text, encoding);
return;
}
catch (IOException ioex)
{
LastException = ioex;
Thread.Sleep(1000);
}
}
// If we get here, re-throw the last exception
throw LastException;
}
/// <summary>
/// FileDelete is an exception ignoring wrapper for System.IO.File.Delete()
/// </summary>
/// <param name="fileName">The name of the file to delete</param>
public static void FileDelete(string fileName)
{
IOException LastException = null;
for (int i = 0; i < 5; i++)
{
try
{
File.Delete(fileName);
return;
}
catch (IOException ioex)
{
LastException = ioex;
Thread.Sleep(1000);
}
}
// If we get here, re-throw the last exception
throw LastException;
}
public static void FileMove(string sourceFileName, string destinationFileName)
{
IOException LastException = null;
for (int i = 0; i < 5; i++)
{
try
{
File.Move(sourceFileName, destinationFileName);
return;
}
catch (IOException ioex)
{
LastException = ioex;
Thread.Sleep(1000);
}
}
// If we get here, re-throw the last exception
throw LastException;
}
public static void FileCopy(string sourceFileName, string destinationFileName)
{
IOException LastException = null;
for (int i = 0; i < 5; i++)
{
try
{
File.Copy(sourceFileName, destinationFileName);
return;
}
catch (IOException ioex)
{
LastException = ioex;
Thread.Sleep(1000);
}
}
// If we get here, re-throw the last exception
throw LastException;
}
public static string[] FileReadAllLines(string fileName)
{
return FileReadAllLines(fileName, Encoding.Default);
}
public static string[] FileReadAllLines(string fileName, Encoding encoding)
{
IOException LastException = null;
for (int i = 0; i < 5; i++)
{
try
{
return File.ReadAllLines(fileName, encoding);
}
catch (IOException ioex)
{
LastException = ioex;
Thread.Sleep(1000);
}
}
// If we get here, re-throw the last exception
throw LastException;
}
public static string FileReadAllText(string fileName)
{
return FileReadAllText(fileName, Encoding.Default);
}
public static string FileReadAllText(string fileName, Encoding encoding)
{
IOException LastException = null;
for (int i = 0; i < 5; i++)
{
try
{
return File.ReadAllText(fileName, encoding);
}
catch (IOException ioex)
{
LastException = ioex;
Thread.Sleep(1000);
}
}
// If we get here, re-throw the last exception
throw LastException;
}
public static void FileWriteAllLines(string fileName, string[] lines)
{
FileWriteAllLines(fileName, lines, Encoding.Default);
}
public static void FileWriteAllLines(string fileName, string[] lines, Encoding encoding)
{
IOException LastException = null;
for (int i = 0; i < 5; i++)
{
try
{
File.WriteAllLines(fileName, lines, encoding);
return;
}
catch (IOException ioex)
{
LastException = ioex;
Thread.Sleep(1000);
}
}
// If we get here, re-throw the last exception
throw LastException;
}
public static void FileWriteAllText(string fileName, string text)
{
FileWriteAllText(fileName, text, Encoding.Default);
}
public static void FileWriteAllText(string fileName, string text, Encoding encoding)
{
IOException LastException = null;
for (int i = 0; i < 5; i++)
{
try
{
File.WriteAllText(fileName, text, encoding);
return;
}
catch (IOException ioex)
{
LastException = ioex;
Thread.Sleep(1000);
}
}
// If we get here, re-throw the last exception
throw LastException;
}
public static byte[] GetFileHash(string fileName)
{
using (FileStream FS = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
return SHA1.Create().ComputeHash(FS);
}
}
public static long GetFileSize(string fileName)
{
try
{
return new FileInfo(fileName).Length;
}
catch (Exception)
{
return 0;
}
}
public static int GetStringCount(string fileName, string searchString, bool continueAfterFirstMatch, bool caseSensitive)
{
RegexOptions RegOptions = (caseSensitive) ? RegexOptions.None : RegexOptions.IgnoreCase;
// Check if we'll do the whole file at once or just a portion
if (new FileInfo(fileName).Length < 100000000) {
RegOptions |= RegexOptions.Singleline;
string FileContents = File.ReadAllText(fileName);
return Regex.Matches(FileContents, searchString, RegOptions).Count;
} else {
bool Abort = false;
string Line = null;
int LineCount = 0;
int MatchCount = 0;
using (StreamReader SR = File.OpenText(fileName)) {
while ((Line = SR.ReadLine()) != null) {
if (Regex.Match(Line, searchString, RegOptions).Success) {
// We have a string match, so increment the match counter, and return if we don't care to find all matches
MatchCount++;
if (!continueAfterFirstMatch) return 1;
}
// Let the main program know our progress every 1000 lines
if (LineCount++ % 1000 == 999) {
DoProgress(SR.BaseStream.Position, SR.BaseStream.Length, out Abort);
if (Abort) return MatchCount;
}
}
}
return MatchCount;
}
}
public static int GetTextPercent(string fileName, int numberOfBytesToAnalyze)
{
int TextBytes = 0;
int TextPercent = 0;
byte[] Buffer = new byte[numberOfBytesToAnalyze];
try
{
using (FileStream FS = new FileStream(fileName, FileMode.Open))
{
int BytesRead = FS.Read(Buffer, 0, Buffer.Length);
for (int i = 0; i < BytesRead; i++)
{
if (((Buffer[i] >= 8) && (Buffer[i] <= 13)) || ((Buffer[i] >= 32) && (Buffer[i] <= 126))) TextBytes++;
}
TextPercent = (int)(TextBytes * 100.0 / BytesRead);
}
}
catch (Exception)
{
// Text Percent failed
}
return TextPercent;
}
}
}