-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtil.cs
More file actions
634 lines (605 loc) · 22.1 KB
/
FileUtil.cs
File metadata and controls
634 lines (605 loc) · 22.1 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Globalization;
namespace MetroUITest
{
class FileUtil
{
/// <summary>
/// 换行符
/// </summary>
public static string NewLine = "\r\n";
#region 检测指定目录是否存在
/// <summary>
/// 检测指定目录是否存在
/// </summary>
/// <param name="directoryPath">目录的绝对路径</param>
public static bool IsExistDirectory(string directoryPath)
{
return Directory.Exists(directoryPath);
}
#endregion
#region 检测指定文件是否存在
/// <summary>
/// 检测指定文件是否存在,如果存在则返回true。
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static bool IsExistFile(string filePath)
{
return File.Exists(filePath);
}
#endregion
#region 检测指定目录是否为空
/// <summary>
/// 检测指定目录是否为空
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
public static bool IsEmptyDirectory(string directoryPath)
{
try
{
//判断是否存在文件
string[] fileNames = GetFileNames(directoryPath);
if (fileNames.Length > 0)
{
return false;
}
//判断是否存在文件夹
string[] directoryNames = GetDirectories(directoryPath);
if (directoryNames.Length > 0)
{
return false;
}
return true;
}
catch (Exception ex)
{
// Log.Logger.Error("FileUtil:IsEmptyDirectory;Common", ex);
return true;
}
}
#endregion
#region 检测指定目录中是否存在指定的文件
/// <summary>
/// 检测指定目录中是否存在指定的文件,若要搜索子目录请使用重载方法.
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
/// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
/// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
public static bool Contains(string directoryPath, string searchPattern)
{
try
{
//获取指定的文件列表
string[] fileNames = GetFileNames(directoryPath, searchPattern, false);
//判断指定文件是否存在
if (fileNames.Length == 0)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
// Log.Logger.Error("FileUtil:Contains();Common", ex);
return false;
}
}
/// <summary>
/// 检测指定目录中是否存在指定的文件
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
/// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
/// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
/// <param name="isSearchChild">是否搜索子目录</param>
public static bool Contains(string directoryPath, string searchPattern, bool isSearchChild)
{
try
{
//获取指定的文件列表
string[] fileNames = GetFileNames(directoryPath, searchPattern, true);
//判断指定文件是否存在
if (fileNames.Length == 0)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
// Log.Logger.Error("FileUtil:Contains();Common", ex);
return false;
}
}
#endregion
#region 创建一个目录
/// <summary>
/// 创建一个目录
/// </summary>
/// <param name="directoryPath">目录的绝对路径</param>
public static void CreateDirectory(string directoryPath)
{
//如果目录不存在则创建该目录
if (!IsExistDirectory(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
}
#endregion
#region 创建一个文件
/// <summary>
/// 创建一个文件。
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static void CreateFile(string filePath)
{
try
{
//如果文件不存在则创建该文件
if (!IsExistFile(filePath))
{
//创建一个FileInfo对象
FileInfo file = new FileInfo(filePath);
//创建文件
FileStream fs = file.Create();
//关闭文件流
fs.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 创建一个文件,并将字节流写入文件。
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
/// <param name="buffer">二进制流数据</param>
public static void CreateFile(string filePath, byte[] buffer)
{
try
{
//如果文件不存在则创建该文件
if (!IsExistFile(filePath))
{
//创建一个FileInfo对象
FileInfo file = new FileInfo(filePath);
//创建文件
FileStream fs = file.Create();
//写入二进制流
fs.Write(buffer, 0, buffer.Length);
//关闭文件流
fs.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 创建一个文件
/// </summary>
/// <param name="filePath">路径</param>
/// <param name="s">内容</param>
/// <param name="encode">编码</param>
/// <returns></returns>
public static bool CreateFile(string filePath, string s)
{
bool ret = true;
StreamWriter sw = null;
try
{
UTF8Encoding utf8 = new UTF8Encoding(false);
using (sw = new StreamWriter(filePath, false, utf8))
{
sw.Write(s);
}
}
catch
{
ret = false;
throw new Exception("存储路径错误,请检查路径" + filePath + "是否存在!");
}
finally
{
sw.Close();
}
return ret;
}
#endregion
#region 获取文本文件的行数
/// <summary>
/// 获取文本文件的行数
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static int GetLineCount(string filePath)
{
//将文本文件的各行读到一个字符串数组中
string[] rows = File.ReadAllLines(filePath);
//返回行数
return rows.Length;
}
#endregion
#region 获取一个文件的长度
/// <summary>
/// 获取一个文件的长度,单位为Byte
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static int GetFileSize(string filePath)
{
//创建一个文件对象
FileInfo fi = new FileInfo(filePath);
//获取文件的大小
return (int)fi.Length;
}
/// <summary>
/// 获取一个文件的长度,单位为KB
/// </summary>
/// <param name="filePath">文件的路径</param>
public static double GetFileSizeByKB(string filePath)
{
//创建一个文件对象
FileInfo fi = new FileInfo(filePath);
//获取文件的大小
return Convert.ToDouble(Convert.ToDouble(fi.Length) / 1024);
}
/// <summary>
/// 获取一个文件的长度,单位为MB
/// </summary>
/// <param name="filePath">文件的路径</param>
public static double GetFileSizeByMB(string filePath)
{
//创建一个文件对象
FileInfo fi = new FileInfo(filePath);
//获取文件的大小
return Convert.ToDouble(Convert.ToDouble(fi.Length) / 1024 / 1024);
}
#endregion
#region 获取指定目录中的文件列表
/// <summary>
/// 获取指定目录中所有文件列表
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
public static string[] GetFileNames(string directoryPath)
{
//如果目录不存在,则抛出异常
if (!IsExistDirectory(directoryPath))
{
throw new FileNotFoundException();
}
//获取文件列表
return Directory.GetFiles(directoryPath);
}
/// <summary>
/// 获取指定目录及子目录中所有文件列表
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
/// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
/// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
/// <param name="isSearchChild">是否搜索子目录</param>
public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild)
{
//如果目录不存在,则抛出异常
if (!IsExistDirectory(directoryPath))
{
throw new FileNotFoundException();
}
try
{
if (isSearchChild)
{
return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories);
}
else
{
return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
}
}
catch (IOException ex)
{
throw ex;
}
}
#endregion
#region 获取指定目录中的子目录列表
/// <summary>
/// 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法.
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
public static string[] GetDirectories(string directoryPath)
{
try
{
return Directory.GetDirectories(directoryPath);
}
catch (IOException ex)
{
throw ex;
}
}
/// <summary>
/// 获取指定目录及子目录中所有子目录列表
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
/// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
/// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
/// <param name="isSearchChild">是否搜索子目录</param>
public static string[] GetDirectories(string directoryPath, string searchPattern, bool isSearchChild)
{
try
{
if (isSearchChild)
{
return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.AllDirectories);
}
else
{
return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
}
}
catch (IOException ex)
{
throw ex;
}
}
#endregion
#region 向文本文件写入内容
/// <summary>
/// 向文本文件中写入内容
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
/// <param name="content">写入的内容</param>
public static void WriteText(string filePath, string content)
{
//向文件写入内容
File.WriteAllText(filePath, content);
}
#endregion
#region 向文本文件的尾部追加内容
/// <summary>
/// 向文本文件的尾部追加内容
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
/// <param name="content">写入的内容</param>
public static void AppendText(string filePath, string content)
{
File.AppendAllText(filePath, content);
}
#endregion
#region 将现有文件的内容复制到新文件中
/// <summary>
/// 将源文件的内容复制到目标文件中
/// </summary>
/// <param name="sourceFilePath">源文件的绝对路径</param>
/// <param name="destFilePath">目标文件的绝对路径</param>
public static void Copy(string sourceFilePath, string destFilePath)
{
File.Copy(sourceFilePath, destFilePath, true);
}
#endregion
#region 将文件移动到指定目录
/// <summary>
/// 将文件移动到指定目录
/// </summary>
/// <param name="sourceFilePath">需要移动的源文件的绝对路径</param>
/// <param name="descDirectoryPath">移动到的目录的绝对路径</param>
public static void Move(string sourceFilePath, string descDirectoryPath)
{
//获取源文件的名称
string sourceFileName = GetFileName(sourceFilePath);
if (IsExistDirectory(descDirectoryPath))
{
//如果目标中存在同名文件,则删除
if (IsExistFile(descDirectoryPath + "\\" + sourceFileName))
{
DeleteFile(descDirectoryPath + "\\" + sourceFileName);
}
//将文件移动到指定目录
File.Move(sourceFilePath, descDirectoryPath + "\\" + sourceFileName);
}
}
#endregion
#region 将流读取到缓冲区中
/// <summary>
/// 将流读取到缓冲区中
/// </summary>
/// <param name="stream">原始流</param>
public static byte[] StreamToBytes(Stream stream)
{
try
{
//创建缓冲区
byte[] buffer = new byte[stream.Length];
//读取流
stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
//返回流
return buffer;
}
catch (Exception ex)
{
throw ex;
}
finally
{
//关闭流
stream.Close();
}
}
#endregion
#region 将文件读取到缓冲区中
/// <summary>
/// 将文件读取到缓冲区中
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static byte[] FileToBytes(string filePath)
{
//获取文件的大小
int fileSize = GetFileSize(filePath);
//创建一个临时缓冲区
byte[] buffer = new byte[fileSize];
//创建一个文件流
FileInfo fi = new FileInfo(filePath);
FileStream fs = fi.Open(FileMode.Open);
try
{
//将文件流读入缓冲区
fs.Read(buffer, 0, fileSize);
return buffer;
}
catch (IOException ex)
{
throw ex;
}
finally
{
//关闭文件流
fs.Close();
}
}
#endregion
#region 将文件读取到字符串中
/// <summary>
/// 将文件读取到字符串中
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static string FileToString(string filePath)
{
return FileToString(filePath, Encoding.Default);
}
/// <summary>
/// 将文件读取到字符串中
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
/// <param name="encoding">字符编码</param>
public static string FileToString(string filePath, Encoding encoding)
{
//创建流读取器
StreamReader reader = new StreamReader(filePath, encoding);
try
{
//读取流
return reader.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
//关闭流读取器
reader.Close();
}
}
#endregion
#region 从文件的绝对路径中获取文件名( 包含扩展名 )
/// <summary>
/// 从文件的绝对路径中获取文件名( 包含扩展名 )
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static string GetFileName(string filePath)
{
//获取文件的名称
FileInfo fi = new FileInfo(filePath);
return fi.Name;
}
#endregion
#region 从文件的绝对路径中获取文件名( 不包含扩展名 )
/// <summary>
/// 从文件的绝对路径中获取文件名( 不包含扩展名 )
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static string GetFileNameNoExtension(string filePath)
{
//获取文件的名称
FileInfo fi = new FileInfo(filePath);
return fi.Name.Split('.')[0];
}
#endregion
#region 从文件的绝对路径中获取扩展名
/// <summary>
/// 从文件的绝对路径中获取扩展名
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static string GetExtension(string filePath)
{
//获取文件的名称
FileInfo fi = new FileInfo(filePath);
return fi.Extension;
}
#endregion
#region 清空指定目录
/// <summary>
/// 清空指定目录下所有文件及子目录,但该目录依然保存.
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
public static void ClearDirectory(string directoryPath)
{
if (IsExistDirectory(directoryPath))
{
//删除目录中所有的文件
string[] fileNames = GetFileNames(directoryPath);
for (int i = 0; i < fileNames.Length; i++)
{
DeleteFile(fileNames[i]);
}
//删除目录中所有的子目录
string[] directoryNames = GetDirectories(directoryPath);
for (int i = 0; i < directoryNames.Length; i++)
{
DeleteDirectory(directoryNames[i]);
}
}
}
#endregion
#region 清空文件内容
/// <summary>
/// 清空文件内容
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static void ClearFile(string filePath)
{
//删除文件
File.Delete(filePath);
//重新创建该文件
CreateFile(filePath);
}
#endregion
#region 删除指定文件
/// <summary>
/// 删除指定文件
/// </summary>
/// <param name="filePath">文件的绝对路径</param>
public static void DeleteFile(string filePath)
{
if (IsExistFile(filePath))
{
File.Delete(filePath);
}
}
#endregion
#region 删除指定目录
/// <summary>
/// 删除指定目录及其所有子目录
/// </summary>
/// <param name="directoryPath">指定目录的绝对路径</param>
public static void DeleteDirectory(string directoryPath)
{
if (IsExistDirectory(directoryPath))
{
Directory.Delete(directoryPath, true);
}
}
#endregion
}
}