|
@@ -1,12 +1,12 @@
|
1
|
|
-using System;
|
2
|
|
-using System.Configuration;
|
3
|
|
-using System.IO;
|
4
|
|
-using System.Text;
|
5
|
|
-using System.Text.RegularExpressions;
|
6
|
|
-using System.Windows.Forms;
|
7
|
|
-
|
8
|
|
-namespace Common.system
|
|
1
|
+namespace XHWK.WKTool.system
|
9
|
2
|
{
|
|
3
|
+ using System;
|
|
4
|
+ using System.Configuration;
|
|
5
|
+ using System.IO;
|
|
6
|
+ using System.Text;
|
|
7
|
+ using System.Text.RegularExpressions;
|
|
8
|
+ using System.Windows.Forms;
|
|
9
|
+
|
10
|
10
|
/// <summary>
|
11
|
11
|
/// 文件工具公共方法
|
12
|
12
|
/// 创建人:赵耀
|
|
@@ -19,94 +19,97 @@ namespace Common.system
|
19
|
19
|
/// <summary>
|
20
|
20
|
/// 获取文件的绝对路径
|
21
|
21
|
/// </summary>
|
22
|
|
- /// <param name="Path">相对路径</param>
|
|
22
|
+ /// <param name="path">相对路径</param>
|
23
|
23
|
/// <returns></returns>
|
24
|
|
- public static string GetFileAbsolutePath(string Path = "")
|
|
24
|
+ public static string GetFileAbsolutePath(string path = "")
|
25
|
25
|
{
|
26
|
26
|
try
|
27
|
27
|
{
|
28
|
|
- if (!string.IsNullOrWhiteSpace(Path))
|
|
28
|
+ if (!string.IsNullOrWhiteSpace(path))
|
29
|
29
|
{
|
30
|
|
- Path = Path.Replace("\\", "/");
|
31
|
|
- if (Path != "/")
|
|
30
|
+ path = path.Replace("\\", "/");
|
|
31
|
+ if (path != "/")
|
32
|
32
|
{
|
33
|
|
- if (Path.Substring(1, 1) == ":")
|
|
33
|
+ if (path.Substring(1, 1) == ":")
|
34
|
34
|
{
|
35
|
|
- return Path;
|
|
35
|
+ return path;
|
36
|
36
|
}
|
37
|
|
-
|
38
|
|
- if (Path.Substring(0, 1) != "/")
|
|
37
|
+ if (path.Substring(0, 1) != "/")
|
39
|
38
|
{
|
40
|
|
- Path = "/" + Path;
|
|
39
|
+ path = "/" + path;
|
41
|
40
|
}
|
42
|
41
|
}
|
43
|
42
|
}
|
44
|
|
- string AbsolutePath = Application.StartupPath.ToString().Replace("\\", "/") + Path;
|
45
|
|
- return AbsolutePath;
|
|
43
|
+ string absolutePath = Application.StartupPath.Replace("\\", "/") + path;
|
|
44
|
+ return absolutePath;
|
46
|
45
|
}
|
47
|
46
|
catch (Exception)
|
48
|
47
|
{
|
49
|
|
- return Path;
|
|
48
|
+ return path;
|
50
|
49
|
}
|
51
|
50
|
}
|
|
51
|
+
|
52
|
52
|
/// <summary>
|
53
|
53
|
/// 获取合法路径 结尾带/
|
54
|
54
|
/// </summary>
|
55
|
55
|
/// <returns></returns>
|
56
|
|
- public static string GetLegalPath(string FilePath)
|
|
56
|
+ public static string GetLegalPath(string filePath)
|
57
|
57
|
{
|
58
|
|
- FilePath = System.IO.Path.Combine(FilePath.Replace("\\", "/"));
|
59
|
|
- FilePath += (FilePath.Substring(FilePath.Length - 1, 1) == "/" ? "" : "/");
|
60
|
|
- return FilePath;
|
|
58
|
+ filePath = System.IO.Path.Combine(filePath.Replace("\\", "/"));
|
|
59
|
+ filePath += (filePath.Substring(filePath.Length - 1, 1) == "/" ? "" : "/");
|
|
60
|
+ return filePath;
|
61
|
61
|
}
|
|
62
|
+
|
62
|
63
|
#endregion
|
63
|
64
|
|
64
|
65
|
#region 检测路径是否合法
|
|
66
|
+
|
65
|
67
|
/// <summary>
|
66
|
68
|
/// 检测路径是否合法
|
67
|
69
|
/// </summary>
|
68
|
|
- /// <param name="FilePathName">绝对路径</param>
|
69
|
|
- /// <param name="ErrMessage">返回错误</param>
|
|
70
|
+ /// <param name="filePathName">绝对路径</param>
|
|
71
|
+ /// <param name="errMessage">返回错误</param>
|
70
|
72
|
/// <returns></returns>
|
71
|
|
- public static bool IsLegalPath(string FilePathName, out string ErrMessage)
|
|
73
|
+ public static bool IsLegalPath(string filePathName, out string errMessage)
|
72
|
74
|
{
|
73
|
|
- string FilePath;
|
74
|
|
- string FileName;
|
75
|
|
- ErrMessage = "";
|
|
75
|
+ string filePath;
|
|
76
|
+ string fileName;
|
|
77
|
+ errMessage = "";
|
76
|
78
|
try
|
77
|
79
|
{
|
78
|
|
- FilePathName = GetLegalPath(FilePathName);
|
79
|
|
- FilePath = GetDirectoryName(FilePathName);
|
80
|
|
- FileName = GetIOFileName(FilePathName);
|
|
80
|
+ filePathName = GetLegalPath(filePathName);
|
|
81
|
+ filePath = GetDirectoryName(filePathName);
|
|
82
|
+ fileName = GetIoFileName(filePathName);
|
81
|
83
|
}
|
82
|
84
|
catch (Exception)
|
83
|
85
|
{
|
84
|
|
- ErrMessage = "路径或文件名不合法!";
|
|
86
|
+ errMessage = "路径或文件名不合法!";
|
85
|
87
|
return false;
|
86
|
88
|
}
|
87
|
89
|
Regex regex = new Regex(@"^([a-zA-Z]:\\)?[^\/\:\*\?\""\<\>\|\,]*$");
|
88
|
|
- Match m = regex.Match(FilePath);
|
|
90
|
+ Match m = regex.Match(filePath);
|
89
|
91
|
if (!m.Success)
|
90
|
92
|
{
|
91
|
|
- ErrMessage = "非法的文件保存路径!";
|
|
93
|
+ errMessage = "非法的文件保存路径!";
|
92
|
94
|
return false;
|
93
|
95
|
}
|
94
|
|
- if (!string.IsNullOrWhiteSpace(FileName))
|
|
96
|
+ if (!string.IsNullOrWhiteSpace(fileName))
|
95
|
97
|
{
|
96
|
98
|
regex = new Regex(@"^[^\/\:\*\?\""\<\>\|\,]+$");
|
97
|
|
- m = regex.Match(FileName);
|
|
99
|
+ m = regex.Match(fileName);
|
98
|
100
|
if (!m.Success)
|
99
|
101
|
{
|
100
|
|
- ErrMessage = "非法的文件名!文件名中包含\\ / : * ? \" < > |等字符!";
|
|
102
|
+ errMessage = "非法的文件名!文件名中包含\\ / : * ? \" < > |等字符!";
|
101
|
103
|
return false;
|
102
|
104
|
}
|
103
|
105
|
}
|
104
|
106
|
return true;
|
105
|
|
-
|
106
|
107
|
}
|
|
108
|
+
|
107
|
109
|
#endregion
|
108
|
110
|
|
109
|
111
|
#region 检测指定目录是否存在
|
|
112
|
+
|
110
|
113
|
/// <summary>
|
111
|
114
|
/// 检测指定目录是否存在
|
112
|
115
|
/// </summary>
|
|
@@ -115,9 +118,11 @@ namespace Common.system
|
115
|
118
|
{
|
116
|
119
|
return Directory.Exists(directoryPath);
|
117
|
120
|
}
|
|
121
|
+
|
118
|
122
|
#endregion
|
119
|
123
|
|
120
|
124
|
#region 检测指定文件是否存在
|
|
125
|
+
|
121
|
126
|
/// <summary>
|
122
|
127
|
/// 检测指定文件是否存在,如果存在则返回true。
|
123
|
128
|
/// </summary>
|
|
@@ -126,9 +131,11 @@ namespace Common.system
|
126
|
131
|
{
|
127
|
132
|
return File.Exists(filePath);
|
128
|
133
|
}
|
|
134
|
+
|
129
|
135
|
#endregion
|
130
|
136
|
|
131
|
137
|
#region 检测指定目录是否为空
|
|
138
|
+
|
132
|
139
|
/// <summary>
|
133
|
140
|
/// 检测指定目录是否为空
|
134
|
141
|
/// </summary>
|
|
@@ -153,9 +160,11 @@ namespace Common.system
|
153
|
160
|
return false;
|
154
|
161
|
}
|
155
|
162
|
}
|
|
163
|
+
|
156
|
164
|
#endregion
|
157
|
165
|
|
158
|
166
|
#region 检测指定目录中是否存在指定的文件
|
|
167
|
+
|
159
|
168
|
/// <summary>
|
160
|
169
|
/// 检测指定目录中是否存在指定的文件,若要搜索子目录请使用重载方法.
|
161
|
170
|
/// </summary>
|
|
@@ -167,7 +176,12 @@ namespace Common.system
|
167
|
176
|
try
|
168
|
177
|
{
|
169
|
178
|
//获取指定的文件列表
|
170
|
|
- string[] fileNames = GetFileNames(directoryPath, searchPattern, false);
|
|
179
|
+ string[] fileNames = GetFileNames
|
|
180
|
+ (
|
|
181
|
+ directoryPath,
|
|
182
|
+ searchPattern,
|
|
183
|
+ false
|
|
184
|
+ );
|
171
|
185
|
|
172
|
186
|
//判断指定文件是否存在
|
173
|
187
|
return fileNames.Length != 0;
|
|
@@ -185,12 +199,22 @@ namespace Common.system
|
185
|
199
|
/// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
|
186
|
200
|
/// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
|
187
|
201
|
/// <param name="isSearchChild">是否搜索子目录</param>
|
188
|
|
- public static bool Contains(string directoryPath, string searchPattern, bool isSearchChild)
|
|
202
|
+ public static bool Contains
|
|
203
|
+ (
|
|
204
|
+ string directoryPath,
|
|
205
|
+ string searchPattern,
|
|
206
|
+ bool isSearchChild
|
|
207
|
+ )
|
189
|
208
|
{
|
190
|
209
|
try
|
191
|
210
|
{
|
192
|
211
|
//获取指定的文件列表
|
193
|
|
- string[] fileNames = GetFileNames(directoryPath, searchPattern, isSearchChild);
|
|
212
|
+ string[] fileNames = GetFileNames
|
|
213
|
+ (
|
|
214
|
+ directoryPath,
|
|
215
|
+ searchPattern,
|
|
216
|
+ isSearchChild
|
|
217
|
+ );
|
194
|
218
|
|
195
|
219
|
//判断指定文件是否存在
|
196
|
220
|
return fileNames.Length != 0;
|
|
@@ -200,9 +224,11 @@ namespace Common.system
|
200
|
224
|
return false;
|
201
|
225
|
}
|
202
|
226
|
}
|
|
227
|
+
|
203
|
228
|
#endregion
|
204
|
229
|
|
205
|
230
|
#region 创建一个目录
|
|
231
|
+
|
206
|
232
|
/// <summary>
|
207
|
233
|
/// 创建一个目录
|
208
|
234
|
/// </summary>
|
|
@@ -222,9 +248,11 @@ namespace Common.system
|
222
|
248
|
throw new ApplicationException(ex.Message); //"请使用管理员权限运行!"
|
223
|
249
|
}
|
224
|
250
|
}
|
|
251
|
+
|
225
|
252
|
#endregion
|
226
|
253
|
|
227
|
254
|
#region 创建一个文件
|
|
255
|
+
|
228
|
256
|
/// <summary>
|
229
|
257
|
/// 创建一个文件。
|
230
|
258
|
/// </summary>
|
|
@@ -248,7 +276,6 @@ namespace Common.system
|
248
|
276
|
{
|
249
|
277
|
return false;
|
250
|
278
|
}
|
251
|
|
-
|
252
|
279
|
return true;
|
253
|
280
|
}
|
254
|
281
|
|
|
@@ -271,7 +298,12 @@ namespace Common.system
|
271
|
298
|
FileStream fs = file.Create();
|
272
|
299
|
|
273
|
300
|
//写入二进制流
|
274
|
|
- fs.Write(buffer, 0, buffer.Length);
|
|
301
|
+ fs.Write
|
|
302
|
+ (
|
|
303
|
+ buffer,
|
|
304
|
+ 0,
|
|
305
|
+ buffer.Length
|
|
306
|
+ );
|
275
|
307
|
|
276
|
308
|
//关闭文件流
|
277
|
309
|
fs.Close();
|
|
@@ -283,9 +315,11 @@ namespace Common.system
|
283
|
315
|
}
|
284
|
316
|
return true;
|
285
|
317
|
}
|
|
318
|
+
|
286
|
319
|
#endregion
|
287
|
320
|
|
288
|
321
|
#region 获取文本文件的行数
|
|
322
|
+
|
289
|
323
|
/// <summary>
|
290
|
324
|
/// 获取文本文件的行数
|
291
|
325
|
/// </summary>
|
|
@@ -298,9 +332,11 @@ namespace Common.system
|
298
|
332
|
//返回行数
|
299
|
333
|
return rows.Length;
|
300
|
334
|
}
|
|
335
|
+
|
301
|
336
|
#endregion
|
302
|
337
|
|
303
|
338
|
#region 获取一个文件的长度
|
|
339
|
+
|
304
|
340
|
/// <summary>
|
305
|
341
|
/// 获取一个文件的长度,单位为Byte
|
306
|
342
|
/// </summary>
|
|
@@ -318,12 +354,17 @@ namespace Common.system
|
318
|
354
|
/// 获取一个文件的长度,单位为KB
|
319
|
355
|
/// </summary>
|
320
|
356
|
/// <param name="filePath">文件的路径</param>
|
321
|
|
- public static double GetFileSizeByKB(string filePath)
|
|
357
|
+ public static double GetFileSizeByKb(string filePath)
|
322
|
358
|
{
|
323
|
359
|
//创建一个文件对象
|
324
|
360
|
FileInfo fi = new FileInfo(filePath);
|
325
|
361
|
//double size = fi.Length / 1024;
|
326
|
|
- double size = Math.Round((fi.Length / 1024.0), 2, MidpointRounding.AwayFromZero);
|
|
362
|
+ double size = Math.Round
|
|
363
|
+ (
|
|
364
|
+ (fi.Length / 1024.0),
|
|
365
|
+ 2,
|
|
366
|
+ MidpointRounding.AwayFromZero
|
|
367
|
+ );
|
327
|
368
|
//获取文件的大小
|
328
|
369
|
return size;
|
329
|
370
|
}
|
|
@@ -332,18 +373,25 @@ namespace Common.system
|
332
|
373
|
/// 获取一个文件的长度,单位为MB
|
333
|
374
|
/// </summary>
|
334
|
375
|
/// <param name="filePath">文件的路径</param>
|
335
|
|
- public static double GetFileSizeByMB(string filePath)
|
|
376
|
+ public static double GetFileSizeByMb(string filePath)
|
336
|
377
|
{
|
337
|
378
|
//创建一个文件对象
|
338
|
379
|
FileInfo fi = new FileInfo(filePath);
|
339
|
380
|
//double size = fi.Length / 1024 / 1024;
|
340
|
|
- double size = Math.Round((fi.Length / 1024.0 / 1024.0), 2, MidpointRounding.AwayFromZero);
|
|
381
|
+ double size = Math.Round
|
|
382
|
+ (
|
|
383
|
+ (fi.Length / 1024.0 / 1024.0),
|
|
384
|
+ 2,
|
|
385
|
+ MidpointRounding.AwayFromZero
|
|
386
|
+ );
|
341
|
387
|
//获取文件的大小
|
342
|
388
|
return size;
|
343
|
389
|
}
|
|
390
|
+
|
344
|
391
|
#endregion
|
345
|
392
|
|
346
|
393
|
#region 获取指定目录中的文件列表
|
|
394
|
+
|
347
|
395
|
/// <summary>
|
348
|
396
|
/// 获取指定目录中所有文件列表
|
349
|
397
|
/// </summary>
|
|
@@ -354,7 +402,7 @@ namespace Common.system
|
354
|
402
|
//如果目录不存在,则抛出异常
|
355
|
403
|
if (!IsExistDirectory(directoryPath))
|
356
|
404
|
{
|
357
|
|
- return null;//throw new FileNotFoundException();
|
|
405
|
+ return null; //throw new FileNotFoundException();
|
358
|
406
|
}
|
359
|
407
|
DirectoryInfo folder = new DirectoryInfo(directoryPath);
|
360
|
408
|
FileInfo[] fileList;
|
|
@@ -379,7 +427,7 @@ namespace Common.system
|
379
|
427
|
//如果目录不存在,则抛出异常
|
380
|
428
|
if (!IsExistDirectory(directoryPath))
|
381
|
429
|
{
|
382
|
|
- return null;//throw new FileNotFoundException();
|
|
430
|
+ return null; //throw new FileNotFoundException();
|
383
|
431
|
}
|
384
|
432
|
|
385
|
433
|
//获取文件列表
|
|
@@ -393,26 +441,37 @@ namespace Common.system
|
393
|
441
|
/// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
|
394
|
442
|
/// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
|
395
|
443
|
/// <param name="isSearchChild">是否搜索子目录</param>
|
396
|
|
- public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild)
|
|
444
|
+ public static string[] GetFileNames
|
|
445
|
+ (
|
|
446
|
+ string directoryPath,
|
|
447
|
+ string searchPattern,
|
|
448
|
+ bool isSearchChild
|
|
449
|
+ )
|
397
|
450
|
{
|
398
|
451
|
//如果目录不存在,则抛出异常
|
399
|
452
|
if (!IsExistDirectory(directoryPath))
|
400
|
453
|
{
|
401
|
|
- return null;//throw new FileNotFoundException();
|
|
454
|
+ return null; //throw new FileNotFoundException();
|
402
|
455
|
}
|
403
|
|
-
|
404
|
456
|
try
|
405
|
457
|
{
|
406
|
|
- return Directory.GetFiles(directoryPath, searchPattern, isSearchChild ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
|
|
458
|
+ return Directory.GetFiles
|
|
459
|
+ (
|
|
460
|
+ directoryPath,
|
|
461
|
+ searchPattern,
|
|
462
|
+ isSearchChild ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly
|
|
463
|
+ );
|
407
|
464
|
}
|
408
|
465
|
catch
|
409
|
466
|
{
|
410
|
467
|
return null;
|
411
|
468
|
}
|
412
|
469
|
}
|
|
470
|
+
|
413
|
471
|
#endregion
|
414
|
472
|
|
415
|
473
|
#region 获取指定目录中的子目录列表
|
|
474
|
+
|
416
|
475
|
/// <summary>
|
417
|
476
|
/// 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法.
|
418
|
477
|
/// </summary>
|
|
@@ -436,20 +495,32 @@ namespace Common.system
|
436
|
495
|
/// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
|
437
|
496
|
/// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
|
438
|
497
|
/// <param name="isSearchChild">是否搜索子目录</param>
|
439
|
|
- public static string[] GetDirectories(string directoryPath, string searchPattern, bool isSearchChild)
|
|
498
|
+ public static string[] GetDirectories
|
|
499
|
+ (
|
|
500
|
+ string directoryPath,
|
|
501
|
+ string searchPattern,
|
|
502
|
+ bool isSearchChild
|
|
503
|
+ )
|
440
|
504
|
{
|
441
|
505
|
try
|
442
|
506
|
{
|
443
|
|
- return Directory.GetDirectories(directoryPath, searchPattern, isSearchChild ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
|
|
507
|
+ return Directory.GetDirectories
|
|
508
|
+ (
|
|
509
|
+ directoryPath,
|
|
510
|
+ searchPattern,
|
|
511
|
+ isSearchChild ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly
|
|
512
|
+ );
|
444
|
513
|
}
|
445
|
514
|
catch
|
446
|
515
|
{
|
447
|
|
- return null;//throw
|
|
516
|
+ return null; //throw
|
448
|
517
|
}
|
449
|
518
|
}
|
|
519
|
+
|
450
|
520
|
#endregion
|
451
|
521
|
|
452
|
522
|
#region 向文本文件写入内容
|
|
523
|
+
|
453
|
524
|
/// <summary>
|
454
|
525
|
/// 向文本文件中写入内容
|
455
|
526
|
/// </summary>
|
|
@@ -460,9 +531,11 @@ namespace Common.system
|
460
|
531
|
//向文件写入内容
|
461
|
532
|
File.WriteAllText(filePath, content);
|
462
|
533
|
}
|
|
534
|
+
|
463
|
535
|
#endregion
|
464
|
536
|
|
465
|
537
|
#region 向文本文件的尾部追加内容
|
|
538
|
+
|
466
|
539
|
/// <summary>
|
467
|
540
|
/// 向文本文件的尾部追加内容
|
468
|
541
|
/// </summary>
|
|
@@ -472,9 +545,11 @@ namespace Common.system
|
472
|
545
|
{
|
473
|
546
|
File.AppendAllText(filePath, content);
|
474
|
547
|
}
|
|
548
|
+
|
475
|
549
|
#endregion
|
476
|
550
|
|
477
|
551
|
#region 将现有文件的内容复制到新文件中
|
|
552
|
+
|
478
|
553
|
/// <summary>
|
479
|
554
|
/// 将源文件的内容复制到目标文件中
|
480
|
555
|
/// </summary>
|
|
@@ -482,11 +557,18 @@ namespace Common.system
|
482
|
557
|
/// <param name="destFilePath">目标文件的绝对路径</param>
|
483
|
558
|
public static void Copy(string sourceFilePath, string destFilePath)
|
484
|
559
|
{
|
485
|
|
- File.Copy(sourceFilePath, destFilePath, true);
|
|
560
|
+ File.Copy
|
|
561
|
+ (
|
|
562
|
+ sourceFilePath,
|
|
563
|
+ destFilePath,
|
|
564
|
+ true
|
|
565
|
+ );
|
486
|
566
|
}
|
|
567
|
+
|
487
|
568
|
#endregion
|
488
|
569
|
|
489
|
570
|
#region 将文件移动到指定目录
|
|
571
|
+
|
490
|
572
|
/// <summary>
|
491
|
573
|
/// 将文件移动到指定目录
|
492
|
574
|
/// </summary>
|
|
@@ -496,7 +578,6 @@ namespace Common.system
|
496
|
578
|
{
|
497
|
579
|
//获取源文件的名称
|
498
|
580
|
string sourceFileName = GetFileName(sourceFilePath);
|
499
|
|
-
|
500
|
581
|
if (IsExistDirectory(descDirectoryPath))
|
501
|
582
|
{
|
502
|
583
|
//如果目标中存在同名文件,则删除
|
|
@@ -508,9 +589,11 @@ namespace Common.system
|
508
|
589
|
File.Move(sourceFilePath, descDirectoryPath + "\\" + sourceFileName);
|
509
|
590
|
}
|
510
|
591
|
}
|
|
592
|
+
|
511
|
593
|
#endregion
|
512
|
594
|
|
513
|
595
|
#region 将流读取到缓冲区中
|
|
596
|
+
|
514
|
597
|
/// <summary>
|
515
|
598
|
/// 将流读取到缓冲区中
|
516
|
599
|
/// </summary>
|
|
@@ -523,7 +606,12 @@ namespace Common.system
|
523
|
606
|
byte[] buffer = new byte[stream.Length];
|
524
|
607
|
|
525
|
608
|
//读取流
|
526
|
|
- stream.Read(buffer, 0, int.Parse(stream.Length.ToString()));
|
|
609
|
+ stream.Read
|
|
610
|
+ (
|
|
611
|
+ buffer,
|
|
612
|
+ 0,
|
|
613
|
+ int.Parse(stream.Length.ToString())
|
|
614
|
+ );
|
527
|
615
|
|
528
|
616
|
//返回流
|
529
|
617
|
return buffer;
|
|
@@ -538,9 +626,11 @@ namespace Common.system
|
538
|
626
|
stream.Close();
|
539
|
627
|
}
|
540
|
628
|
}
|
|
629
|
+
|
541
|
630
|
#endregion
|
542
|
631
|
|
543
|
632
|
#region 将文件读取到缓冲区中
|
|
633
|
+
|
544
|
634
|
/// <summary>
|
545
|
635
|
/// 将文件读取到缓冲区中
|
546
|
636
|
/// </summary>
|
|
@@ -556,12 +646,15 @@ namespace Common.system
|
556
|
646
|
//创建一个文件流
|
557
|
647
|
FileInfo fi = new FileInfo(filePath);
|
558
|
648
|
FileStream fs = fi.Open(FileMode.Open);
|
559
|
|
-
|
560
|
649
|
try
|
561
|
650
|
{
|
562
|
651
|
//将文件流读入缓冲区
|
563
|
|
- fs.Read(buffer, 0, (int)fileSize);
|
564
|
|
-
|
|
652
|
+ fs.Read
|
|
653
|
+ (
|
|
654
|
+ buffer,
|
|
655
|
+ 0,
|
|
656
|
+ (int)fileSize
|
|
657
|
+ );
|
565
|
658
|
return buffer;
|
566
|
659
|
}
|
567
|
660
|
catch
|
|
@@ -574,46 +667,65 @@ namespace Common.system
|
574
|
667
|
fs.Close();
|
575
|
668
|
}
|
576
|
669
|
}
|
|
670
|
+
|
577
|
671
|
/// <summary>
|
578
|
672
|
/// 读取大文件用,读取文件前面指定长度字节数
|
579
|
673
|
/// </summary>
|
580
|
674
|
/// <param name="filePath">文件路径</param>
|
581
|
|
- /// <param name="Length">读取长度,单位字节</param>
|
|
675
|
+ /// <param name="length">读取长度,单位字节</param>
|
582
|
676
|
/// <returns></returns>
|
583
|
|
- public static byte[] ReadBigFile(string filePath, int Length)
|
|
677
|
+ public static byte[] ReadBigFile(string filePath, int length)
|
584
|
678
|
{
|
585
|
679
|
FileStream stream = new FileStream(filePath, FileMode.Open);
|
586
|
|
- byte[] buffer = new byte[Length];
|
|
680
|
+ byte[] buffer = new byte[length];
|
587
|
681
|
//stream.Position = startIndex;
|
588
|
|
- stream.Read(buffer, 0, Length);
|
|
682
|
+ stream.Read
|
|
683
|
+ (
|
|
684
|
+ buffer,
|
|
685
|
+ 0,
|
|
686
|
+ length
|
|
687
|
+ );
|
589
|
688
|
stream.Close();
|
590
|
689
|
stream.Dispose();
|
591
|
690
|
return buffer;
|
592
|
691
|
}
|
|
692
|
+
|
593
|
693
|
/// <summary>
|
594
|
694
|
/// 读取指定长度的流
|
595
|
695
|
/// </summary>
|
596
|
696
|
/// <param name="filePath">文件位置</param>
|
597
|
697
|
/// <param name="startIndex">开始位置</param>
|
598
|
|
- /// <param name="Length">长度</param>
|
|
698
|
+ /// <param name="length">长度</param>
|
599
|
699
|
/// <returns></returns>
|
600
|
|
- public static byte[] ReadBigFileSpecifyLength(string filePath, long startIndex, int Length)
|
|
700
|
+ public static byte[] ReadBigFileSpecifyLength
|
|
701
|
+ (
|
|
702
|
+ string filePath,
|
|
703
|
+ long startIndex,
|
|
704
|
+ int length
|
|
705
|
+ )
|
601
|
706
|
{
|
602
|
707
|
FileStream stream = new FileStream(filePath, FileMode.Open);
|
603
|
|
- if (startIndex + Length + 1 > stream.Length)
|
|
708
|
+ if (startIndex + length + 1 > stream.Length)
|
604
|
709
|
{
|
605
|
|
- Length = (int)(stream.Length - startIndex);
|
|
710
|
+ length = (int)(stream.Length - startIndex);
|
606
|
711
|
}
|
607
|
|
- byte[] buffer = new byte[Length];
|
|
712
|
+ byte[] buffer = new byte[length];
|
608
|
713
|
stream.Position = startIndex;
|
609
|
|
- stream.Read(buffer, 0, Length);
|
|
714
|
+ stream.Read
|
|
715
|
+ (
|
|
716
|
+ buffer,
|
|
717
|
+ 0,
|
|
718
|
+ length
|
|
719
|
+ );
|
610
|
720
|
stream.Close();
|
611
|
721
|
stream.Dispose();
|
612
|
722
|
return buffer;
|
613
|
723
|
}
|
|
724
|
+
|
614
|
725
|
#endregion
|
615
|
726
|
|
616
|
727
|
#region 将文件读取到字符串中
|
|
728
|
+
|
617
|
729
|
/// <summary>
|
618
|
730
|
/// 将文件读取到字符串中
|
619
|
731
|
/// </summary>
|
|
@@ -623,6 +735,7 @@ namespace Common.system
|
623
|
735
|
//return FileToString(filePath, Encoding.Default);
|
624
|
736
|
return FileToString(filePath, Encoding.UTF8);
|
625
|
737
|
}
|
|
738
|
+
|
626
|
739
|
/// <summary>
|
627
|
740
|
/// 将文件读取到字符串中
|
628
|
741
|
/// </summary>
|
|
@@ -647,21 +760,23 @@ namespace Common.system
|
647
|
760
|
reader.Close();
|
648
|
761
|
}
|
649
|
762
|
}
|
|
763
|
+
|
650
|
764
|
/// <summary>
|
651
|
765
|
/// 读取大文件用,读取文件前面指定长度字节的字符串
|
652
|
766
|
/// </summary>
|
653
|
767
|
/// <param name="filePath">文件路径</param>
|
654
|
|
- /// <param name="Length">读取长度,单位字节</param>
|
|
768
|
+ /// <param name="length">读取长度,单位字节</param>
|
655
|
769
|
/// <returns></returns>
|
656
|
|
- public static string ReadBigFileStr(string filePath, int Length)
|
|
770
|
+ public static string ReadBigFileStr(string filePath, int length)
|
657
|
771
|
{
|
658
|
|
- byte[] buffer = ReadBigFile(filePath, Length);
|
|
772
|
+ byte[] buffer = ReadBigFile(filePath, length);
|
659
|
773
|
return Encoding.Default.GetString(buffer);
|
660
|
774
|
}
|
661
|
775
|
|
662
|
776
|
#endregion
|
663
|
777
|
|
664
|
778
|
#region 从文件的绝对路径中获取文件名( 包含扩展名 )
|
|
779
|
+
|
665
|
780
|
/// <summary>
|
666
|
781
|
/// 从文件的绝对路径中获取文件名( 包含扩展名 )
|
667
|
782
|
/// </summary>
|
|
@@ -672,17 +787,20 @@ namespace Common.system
|
672
|
787
|
FileInfo fi = new FileInfo(filePath);
|
673
|
788
|
return fi.Name;
|
674
|
789
|
}
|
|
790
|
+
|
675
|
791
|
/// <summary>
|
676
|
792
|
/// 使用IO从文件的绝对路径中获取文件名( 包含扩展名 )
|
677
|
793
|
/// </summary>
|
678
|
794
|
/// <param name="filePath">文件的绝对路径</param>
|
679
|
|
- public static string GetIOFileName(string filePath)
|
|
795
|
+ public static string GetIoFileName(string filePath)
|
680
|
796
|
{
|
681
|
797
|
return Path.GetFileName(filePath);
|
682
|
798
|
}
|
|
799
|
+
|
683
|
800
|
#endregion
|
684
|
801
|
|
685
|
802
|
#region 从文件的绝对路径中获取文件名( 不包含扩展名 )
|
|
803
|
+
|
686
|
804
|
/// <summary>
|
687
|
805
|
/// 从文件的绝对路径中获取文件名( 不包含扩展名 )
|
688
|
806
|
/// </summary>
|
|
@@ -693,17 +811,20 @@ namespace Common.system
|
693
|
811
|
FileInfo fi = new FileInfo(filePath);
|
694
|
812
|
return fi.Name.Split('.')[0];
|
695
|
813
|
}
|
|
814
|
+
|
696
|
815
|
/// <summary>
|
697
|
816
|
/// 使用IO从文件的绝对路径中获取文件名( 不包含扩展名 )
|
698
|
817
|
/// </summary>
|
699
|
818
|
/// <param name="filePath">文件的绝对路径</param>
|
700
|
|
- public static string GetIOFileNameNoExtension(string filePath)
|
|
819
|
+ public static string GetIoFileNameNoExtension(string filePath)
|
701
|
820
|
{
|
702
|
821
|
return System.IO.Path.GetFileNameWithoutExtension(filePath);
|
703
|
822
|
}
|
|
823
|
+
|
704
|
824
|
#endregion
|
705
|
825
|
|
706
|
826
|
#region 从文件的绝对路径中获取扩展名
|
|
827
|
+
|
707
|
828
|
/// <summary>
|
708
|
829
|
/// 从文件的绝对路径中获取扩展名
|
709
|
830
|
/// </summary>
|
|
@@ -714,31 +835,35 @@ namespace Common.system
|
714
|
835
|
FileInfo fi = new FileInfo(filePath);
|
715
|
836
|
return fi.Extension;
|
716
|
837
|
}
|
|
838
|
+
|
717
|
839
|
/// <summary>
|
718
|
840
|
/// 使用IO从文件的绝对路径中获取扩展名
|
719
|
841
|
/// </summary>
|
720
|
842
|
/// <param name="filePath">文件的绝对路径</param>
|
721
|
|
- public static string GetIOExtension(string filePath)
|
|
843
|
+ public static string GetIoExtension(string filePath)
|
722
|
844
|
{
|
723
|
|
- string Extension = System.IO.Path.GetExtension(filePath);
|
724
|
|
- return Extension;
|
|
845
|
+ string extension = System.IO.Path.GetExtension(filePath);
|
|
846
|
+ return extension;
|
725
|
847
|
}
|
726
|
848
|
|
727
|
849
|
#endregion
|
728
|
850
|
|
729
|
851
|
#region 从文件的绝对路径中获取路径名
|
|
852
|
+
|
730
|
853
|
/// <summary>
|
731
|
854
|
/// 使用IO从文件的绝对路径中获取路径名
|
732
|
855
|
/// </summary>
|
733
|
856
|
/// <param name="filePath">文件的绝对路径</param>
|
734
|
857
|
public static string GetDirectoryName(string filePath)
|
735
|
858
|
{
|
736
|
|
- string DirectoryName = (System.IO.Path.GetDirectoryName(filePath) + @"/").Replace("\\", "/");
|
737
|
|
- return DirectoryName;
|
|
859
|
+ string directoryName = (System.IO.Path.GetDirectoryName(filePath) + @"/").Replace("\\", "/");
|
|
860
|
+ return directoryName;
|
738
|
861
|
}
|
|
862
|
+
|
739
|
863
|
#endregion
|
740
|
864
|
|
741
|
865
|
#region 清空指定目录
|
|
866
|
+
|
742
|
867
|
/// <summary>
|
743
|
868
|
/// 清空指定目录下所有文件及子目录,但该目录依然保存.
|
744
|
869
|
/// </summary>
|
|
@@ -762,9 +887,11 @@ namespace Common.system
|
762
|
887
|
}
|
763
|
888
|
}
|
764
|
889
|
}
|
|
890
|
+
|
765
|
891
|
#endregion
|
766
|
892
|
|
767
|
893
|
#region 清空文件内容
|
|
894
|
+
|
768
|
895
|
/// <summary>
|
769
|
896
|
/// 清空文件内容
|
770
|
897
|
/// </summary>
|
|
@@ -777,9 +904,11 @@ namespace Common.system
|
777
|
904
|
//重新创建该文件
|
778
|
905
|
CreateFile(filePath);
|
779
|
906
|
}
|
|
907
|
+
|
780
|
908
|
#endregion
|
781
|
909
|
|
782
|
910
|
#region 删除指定文件
|
|
911
|
+
|
783
|
912
|
/// <summary>
|
784
|
913
|
/// 删除指定文件
|
785
|
914
|
/// </summary>
|
|
@@ -795,12 +924,14 @@ namespace Common.system
|
795
|
924
|
}
|
796
|
925
|
catch (Exception)
|
797
|
926
|
{
|
798
|
|
-
|
|
927
|
+ // ignored
|
799
|
928
|
}
|
800
|
929
|
}
|
|
930
|
+
|
801
|
931
|
#endregion
|
802
|
932
|
|
803
|
933
|
#region 删除指定目录
|
|
934
|
+
|
804
|
935
|
/// <summary>
|
805
|
936
|
/// 删除指定目录及其所有子目录
|
806
|
937
|
/// </summary>
|
|
@@ -821,9 +952,11 @@ namespace Common.system
|
821
|
952
|
return false;
|
822
|
953
|
}
|
823
|
954
|
}
|
|
955
|
+
|
824
|
956
|
#endregion
|
825
|
957
|
|
826
|
958
|
#region 获取指定目录大小
|
|
959
|
+
|
827
|
960
|
/// <summary>
|
828
|
961
|
/// 获取目录大小
|
829
|
962
|
/// </summary>
|
|
@@ -832,12 +965,12 @@ namespace Common.system
|
832
|
965
|
public static double PathSize(string directoryPath)
|
833
|
966
|
{
|
834
|
967
|
DirectoryInfo folder = new DirectoryInfo(directoryPath);
|
835
|
|
- double DirectorySize = DirSize(folder);
|
|
968
|
+ double directorySize = DirSize(folder);
|
836
|
969
|
for (int i = 0; i < 2; i++)
|
837
|
970
|
{
|
838
|
|
- DirectorySize /= 1024;
|
|
971
|
+ directorySize /= 1024;
|
839
|
972
|
}
|
840
|
|
- return DirectorySize;
|
|
973
|
+ return directorySize;
|
841
|
974
|
}
|
842
|
975
|
|
843
|
976
|
/// <summary>
|
|
@@ -847,44 +980,51 @@ namespace Common.system
|
847
|
980
|
/// <returns>字节</returns>
|
848
|
981
|
public static long DirSize(DirectoryInfo d)
|
849
|
982
|
{
|
850
|
|
- long Size = 0;
|
|
983
|
+ long size = 0;
|
851
|
984
|
// 所有文件大小.
|
852
|
985
|
FileInfo[] fis = d.GetFiles();
|
853
|
986
|
foreach (FileInfo fi in fis)
|
854
|
987
|
{
|
855
|
|
- Size += fi.Length;
|
|
988
|
+ size += fi.Length;
|
856
|
989
|
}
|
857
|
990
|
// 遍历出当前目录的所有文件夹.
|
858
|
991
|
DirectoryInfo[] dis = d.GetDirectories();
|
859
|
992
|
foreach (DirectoryInfo di in dis)
|
860
|
993
|
{
|
861
|
|
- Size += DirSize(di); //这就用到递归了,调用父方法,注意,这里并不是直接返回值,而是调用父返回来的
|
|
994
|
+ size += DirSize(di); //这就用到递归了,调用父方法,注意,这里并不是直接返回值,而是调用父返回来的
|
862
|
995
|
}
|
863
|
|
- return (Size);
|
|
996
|
+ return (size);
|
864
|
997
|
}
|
|
998
|
+
|
865
|
999
|
#endregion
|
866
|
1000
|
|
867
|
1001
|
#region 移动目录
|
|
1002
|
+
|
868
|
1003
|
/// <summary>
|
869
|
1004
|
/// 移动目录并重命名
|
870
|
1005
|
/// </summary>
|
871
|
1006
|
/// <param name="directoryPath">操作目录绝对路径</param>
|
872
|
1007
|
/// <param name="newDirectoryPath">目标目录绝对路径</param>
|
873
|
|
- /// <param name="Message">返回错误消息,成功则为空</param>
|
|
1008
|
+ /// <param name="message">返回错误消息,成功则为空</param>
|
874
|
1009
|
/// <returns></returns>
|
875
|
|
- public static bool MoveDirectory(string directoryPath, string newDirectoryPath, out string Message)
|
|
1010
|
+ public static bool MoveDirectory
|
|
1011
|
+ (
|
|
1012
|
+ string directoryPath,
|
|
1013
|
+ string newDirectoryPath,
|
|
1014
|
+ out string message
|
|
1015
|
+ )
|
876
|
1016
|
{
|
877
|
|
- Message = "";
|
|
1017
|
+ message = "";
|
878
|
1018
|
try
|
879
|
1019
|
{
|
880
|
|
- if (!IsExistDirectory(directoryPath))//判断目录是否存在
|
|
1020
|
+ if (!IsExistDirectory(directoryPath)) //判断目录是否存在
|
881
|
1021
|
{
|
882
|
|
- Message = "操作目录不存在!";
|
|
1022
|
+ message = "操作目录不存在!";
|
883
|
1023
|
return false;
|
884
|
1024
|
}
|
885
|
1025
|
if (IsExistDirectory(newDirectoryPath))
|
886
|
1026
|
{
|
887
|
|
- Message = "目标目录已存在!";
|
|
1027
|
+ message = "目标目录已存在!";
|
888
|
1028
|
return false;
|
889
|
1029
|
}
|
890
|
1030
|
Directory.Move(directoryPath, newDirectoryPath);
|
|
@@ -892,14 +1032,17 @@ namespace Common.system
|
892
|
1032
|
}
|
893
|
1033
|
catch (Exception ex)
|
894
|
1034
|
{
|
895
|
|
- Message = ex.Message;
|
|
1035
|
+ message = ex.Message;
|
896
|
1036
|
return false;
|
897
|
1037
|
}
|
898
|
1038
|
}
|
|
1039
|
+
|
899
|
1040
|
#endregion
|
900
|
1041
|
|
901
|
1042
|
#region 操作配置文件
|
|
1043
|
+
|
902
|
1044
|
#region 从配置文件获取Value
|
|
1045
|
+
|
903
|
1046
|
/// <summary>
|
904
|
1047
|
/// 从配置文件获取Value
|
905
|
1048
|
/// </summary>
|
|
@@ -919,9 +1062,11 @@ namespace Common.system
|
919
|
1062
|
return "";
|
920
|
1063
|
}
|
921
|
1064
|
}
|
|
1065
|
+
|
922
|
1066
|
#endregion
|
923
|
1067
|
|
924
|
1068
|
#region 设置配置文件
|
|
1069
|
+
|
925
|
1070
|
/// <summary>
|
926
|
1071
|
/// 设置配置文件
|
927
|
1072
|
/// </summary>
|
|
@@ -938,7 +1083,6 @@ namespace Common.system
|
938
|
1083
|
AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
|
939
|
1084
|
appsection.Settings[key].Value = value;
|
940
|
1085
|
config.Save();
|
941
|
|
-
|
942
|
1086
|
return true;
|
943
|
1087
|
}
|
944
|
1088
|
catch
|
|
@@ -946,7 +1090,9 @@ namespace Common.system
|
946
|
1090
|
return false;
|
947
|
1091
|
}
|
948
|
1092
|
}
|
|
1093
|
+
|
949
|
1094
|
#endregion
|
|
1095
|
+
|
950
|
1096
|
#endregion
|
951
|
1097
|
|
952
|
1098
|
#region 获取媒体文件的播放时长
|
|
@@ -954,6 +1100,7 @@ namespace Common.system
|
954
|
1100
|
#endregion
|
955
|
1101
|
|
956
|
1102
|
#region 检测是否安装了某个程序
|
|
1103
|
+
|
957
|
1104
|
/// <summary>
|
958
|
1105
|
/// 确认电脑上是否安装有某个程序
|
959
|
1106
|
/// </summary>
|
|
@@ -961,25 +1108,36 @@ namespace Common.system
|
961
|
1108
|
/// <returns>true: 有安裝, false:沒有安裝</returns>
|
962
|
1109
|
public static bool CheckSoftWartInstallState(string softWareName)
|
963
|
1110
|
{
|
964
|
|
- Microsoft.Win32.RegistryKey uninstallNode =
|
965
|
|
- Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.ReadKey);
|
966
|
|
- foreach (string subKeyName in uninstallNode.GetSubKeyNames())
|
|
1111
|
+ Microsoft.Win32.RegistryKey uninstallNode = Microsoft.Win32.Registry.LocalMachine.OpenSubKey
|
|
1112
|
+ (
|
|
1113
|
+ @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
|
|
1114
|
+ Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree,
|
|
1115
|
+ System.Security.AccessControl.RegistryRights.ReadKey
|
|
1116
|
+ );
|
|
1117
|
+ if (uninstallNode != null)
|
967
|
1118
|
{
|
968
|
|
- //Console.WriteLine(" subKeyName:" + subKeyName);
|
969
|
|
- Microsoft.Win32.RegistryKey subKey = uninstallNode.OpenSubKey(subKeyName);
|
970
|
|
- object displayName = subKey.GetValue("DisplayName");
|
971
|
|
- if (displayName != null)
|
|
1119
|
+ foreach (string subKeyName in uninstallNode.GetSubKeyNames())
|
972
|
1120
|
{
|
973
|
|
- if (displayName.ToString().Contains(softWareName))
|
|
1121
|
+ //Console.WriteLine(" subKeyName:" + subKeyName);
|
|
1122
|
+ Microsoft.Win32.RegistryKey subKey = uninstallNode.OpenSubKey(subKeyName);
|
|
1123
|
+ if (subKey != null)
|
974
|
1124
|
{
|
975
|
|
- //Console.WriteLine(uninstallNode.Name+" subKeyName:" + subKeyName);
|
976
|
|
- return true;
|
977
|
|
- // MessageWindow.Show(displayName.ToString());
|
|
1125
|
+ object displayName = subKey.GetValue("DisplayName");
|
|
1126
|
+ if (displayName != null)
|
|
1127
|
+ {
|
|
1128
|
+ if (displayName.ToString().Contains(softWareName))
|
|
1129
|
+ {
|
|
1130
|
+ //Console.WriteLine(uninstallNode.Name+" subKeyName:" + subKeyName);
|
|
1131
|
+ return true;
|
|
1132
|
+ // MessageWindow.Show(displayName.ToString());
|
|
1133
|
+ }
|
|
1134
|
+ }
|
978
|
1135
|
}
|
979
|
1136
|
}
|
980
|
1137
|
}
|
981
|
1138
|
return false;
|
982
|
1139
|
}
|
|
1140
|
+
|
983
|
1141
|
#endregion
|
984
|
1142
|
}
|
985
|
1143
|
}
|