1
- namespace Util . Helpers ;
1
+ namespace Util . Helpers ;
2
2
3
3
/// <summary>
4
4
/// 文件流操作
@@ -33,7 +33,7 @@ public static byte[] ToBytes( string data ) {
33
33
/// <param name="encoding">字符编码</param>
34
34
public static byte [ ] ToBytes ( string data , Encoding encoding ) {
35
35
if ( string . IsNullOrWhiteSpace ( data ) )
36
- return new byte [ ] { } ;
36
+ return Array . Empty < byte > ( ) ;
37
37
return encoding . GetBytes ( data ) ;
38
38
}
39
39
@@ -136,7 +136,7 @@ public static string ReadToString( string filePath ) {
136
136
/// <param name="filePath">文件绝对路径</param>
137
137
/// <param name="encoding">字符编码</param>
138
138
public static string ReadToString ( string filePath , Encoding encoding ) {
139
- if ( System . IO . File . Exists ( filePath ) == false )
139
+ if ( System . IO . File . Exists ( filePath ) == false )
140
140
return string . Empty ;
141
141
using var reader = new StreamReader ( filePath , encoding ) ;
142
142
return reader . ReadToEnd ( ) ;
@@ -182,7 +182,7 @@ public static async Task<string> ReadToStringAsync( string filePath ) {
182
182
/// <param name="filePath">文件绝对路径</param>
183
183
/// <param name="encoding">字符编码</param>
184
184
public static async Task < string > ReadToStringAsync ( string filePath , Encoding encoding ) {
185
- if ( System . IO . File . Exists ( filePath ) == false )
185
+ if ( System . IO . File . Exists ( filePath ) == false )
186
186
return string . Empty ;
187
187
using var reader = new StreamReader ( filePath , encoding ) ;
188
188
return await reader . ReadToEndAsync ( ) ;
@@ -295,18 +295,34 @@ public static byte[] ReadToBytes( Stream stream ) {
295
295
/// <param name="filePath">文件绝对路径</param>
296
296
/// <param name="content">内容</param>
297
297
public static void Write ( string filePath , string content ) {
298
+ if ( content . IsEmpty ( ) )
299
+ return ;
298
300
Write ( filePath , Convert . ToBytes ( content ) ) ;
299
301
}
300
302
303
+ /// <summary>
304
+ /// 将流写入文件
305
+ /// </summary>
306
+ /// <param name="filePath">文件绝对路径</param>
307
+ /// <param name="content">内容</param>
308
+ public static void Write ( string filePath , Stream content ) {
309
+ if ( content == null )
310
+ return ;
311
+ using ( content ) {
312
+ var bytes = ToBytes ( content ) ;
313
+ Write ( filePath , bytes ) ;
314
+ }
315
+ }
316
+
301
317
/// <summary>
302
318
/// 将字节流写入文件
303
319
/// </summary>
304
320
/// <param name="filePath">文件绝对路径</param>
305
321
/// <param name="content">内容</param>
306
322
public static void Write ( string filePath , byte [ ] content ) {
307
- if ( string . IsNullOrWhiteSpace ( filePath ) )
323
+ if ( string . IsNullOrWhiteSpace ( filePath ) )
308
324
return ;
309
- if ( content == null )
325
+ if ( content == null )
310
326
return ;
311
327
CreateDirectory ( filePath ) ;
312
328
System . IO . File . WriteAllBytes ( filePath , content ) ;
@@ -323,6 +339,8 @@ public static void Write( string filePath, byte[] content ) {
323
339
/// <param name="content">内容</param>
324
340
/// <param name="cancellationToken">取消令牌</param>
325
341
public static async Task WriteAsync ( string filePath , string content , CancellationToken cancellationToken = default ) {
342
+ if ( content . IsEmpty ( ) )
343
+ return ;
326
344
await WriteAsync ( filePath , Convert . ToBytes ( content ) , cancellationToken ) ;
327
345
}
328
346
@@ -333,8 +351,12 @@ public static void Write( string filePath, byte[] content ) {
333
351
/// <param name="content">内容</param>
334
352
/// <param name="cancellationToken">取消令牌</param>
335
353
public static async Task WriteAsync ( string filePath , Stream content , CancellationToken cancellationToken = default ) {
336
- var bytes = await ToBytesAsync ( content , cancellationToken ) ;
337
- await WriteAsync ( filePath , bytes , cancellationToken ) ;
354
+ if ( content == null )
355
+ return ;
356
+ await using ( content ) {
357
+ var bytes = await ToBytesAsync ( content , cancellationToken ) ;
358
+ await WriteAsync ( filePath , bytes , cancellationToken ) ;
359
+ }
338
360
}
339
361
340
362
/// <summary>
@@ -344,9 +366,9 @@ public static void Write( string filePath, byte[] content ) {
344
366
/// <param name="content">内容</param>
345
367
/// <param name="cancellationToken">取消令牌</param>
346
368
public static async Task WriteAsync ( string filePath , byte [ ] content , CancellationToken cancellationToken = default ) {
347
- if ( string . IsNullOrWhiteSpace ( filePath ) )
369
+ if ( string . IsNullOrWhiteSpace ( filePath ) )
348
370
return ;
349
- if ( content == null )
371
+ if ( content == null )
350
372
return ;
351
373
CreateDirectory ( filePath ) ;
352
374
await System . IO . File . WriteAllBytesAsync ( filePath , content , cancellationToken ) ;
@@ -361,7 +383,7 @@ public static void Write( string filePath, byte[] content ) {
361
383
/// </summary>
362
384
/// <param name="filePaths">文件绝对路径集合</param>
363
385
public static void Delete ( IEnumerable < string > filePaths ) {
364
- foreach ( var filePath in filePaths )
386
+ foreach ( var filePath in filePaths )
365
387
Delete ( filePath ) ;
366
388
}
367
389
@@ -370,9 +392,9 @@ public static void Delete( IEnumerable<string> filePaths ) {
370
392
/// </summary>
371
393
/// <param name="filePath">文件绝对路径</param>
372
394
public static void Delete ( string filePath ) {
373
- if ( string . IsNullOrWhiteSpace ( filePath ) )
395
+ if ( string . IsNullOrWhiteSpace ( filePath ) )
374
396
return ;
375
- if ( System . IO . File . Exists ( filePath ) )
397
+ if ( System . IO . File . Exists ( filePath ) )
376
398
System . IO . File . Delete ( filePath ) ;
377
399
}
378
400
@@ -385,7 +407,7 @@ public static void Delete( string filePath ) {
385
407
/// </summary>
386
408
/// <param name="path">目录路径</param>
387
409
/// <param name="searchPattern">搜索模式</param>
388
- public static List < FileInfo > GetAllFiles ( string path , string searchPattern ) {
410
+ public static List < FileInfo > GetAllFiles ( string path , string searchPattern ) {
389
411
return Directory . GetFiles ( path , searchPattern , SearchOption . AllDirectories )
390
412
. Select ( filePath => new FileInfo ( filePath ) ) . ToList ( ) ;
391
413
}
@@ -400,10 +422,10 @@ public static List<FileInfo> GetAllFiles( string path,string searchPattern ) {
400
422
/// <param name="sourceFilePath">源文件绝对路径</param>
401
423
/// <param name="destinationFilePath">目标文件绝对路径</param>
402
424
/// <param name="overwrite">目标文件存在时是否覆盖,默认值: false</param>
403
- public static void Copy ( string sourceFilePath , string destinationFilePath , bool overwrite = false ) {
425
+ public static void Copy ( string sourceFilePath , string destinationFilePath , bool overwrite = false ) {
404
426
if ( sourceFilePath . IsEmpty ( ) || destinationFilePath . IsEmpty ( ) )
405
427
return ;
406
- if ( FileExists ( sourceFilePath ) == false )
428
+ if ( FileExists ( sourceFilePath ) == false )
407
429
return ;
408
430
CreateDirectory ( destinationFilePath ) ;
409
431
System . IO . File . Copy ( sourceFilePath , destinationFilePath , overwrite ) ;
@@ -420,9 +442,9 @@ public static void Copy( string sourceFilePath,string destinationFilePath, bool
420
442
/// <param name="destinationFilePath">目标文件绝对路径</param>
421
443
/// <param name="overwrite">目标文件存在时是否覆盖,默认值: false</param>
422
444
public static void Move ( string sourceFilePath , string destinationFilePath , bool overwrite = false ) {
423
- if ( sourceFilePath . IsEmpty ( ) || destinationFilePath . IsEmpty ( ) )
445
+ if ( sourceFilePath . IsEmpty ( ) || destinationFilePath . IsEmpty ( ) )
424
446
return ;
425
- if ( FileExists ( sourceFilePath ) == false )
447
+ if ( FileExists ( sourceFilePath ) == false )
426
448
return ;
427
449
CreateDirectory ( destinationFilePath ) ;
428
450
System . IO . File . Move ( sourceFilePath , destinationFilePath , overwrite ) ;
0 commit comments