1
- using FluentExtensions . Enumerators ;
2
- using System ;
1
+ using System ;
2
+ using System . Collections . Generic ;
3
3
using System . Globalization ;
4
4
using System . IO ;
5
5
using System . Linq ;
6
6
using System . Text ;
7
7
using System . Threading . Tasks ;
8
+ using FluentExtensions . Enumerators ;
8
9
9
10
namespace FluentExtensions
10
11
{
@@ -370,9 +371,10 @@ public static string DriveFreeSpace(this string driveLetter)
370
371
{
371
372
if ( drive . Name . ToLower ( ) . Replace ( @":\" , "" ) != driveLetter . ToLower ( ) )
372
373
continue ;
373
- var kb = ( int ) ( drive . AvailableFreeSpace * 0.001 ) ;
374
+ var kb = ( int ) ( drive . AvailableFreeSpace * 0.001 ) ;
374
375
freeSpace = kb . ToGB ( DigitalStorage . KB ) ;
375
376
}
377
+
376
378
return Math . Round ( freeSpace , 3 ) + " GB" ;
377
379
}
378
380
@@ -389,9 +391,10 @@ public static string DriveTotalSize(this string driveLetter)
389
391
{
390
392
if ( drive . Name . ToLower ( ) . Replace ( @":\" , "" ) != driveLetter . ToLower ( ) )
391
393
continue ;
392
- var kb = ( int ) ( drive . TotalSize * 0.001 ) ;
394
+ var kb = ( int ) ( drive . TotalSize * 0.001 ) ;
393
395
freeSpace = kb . ToGB ( DigitalStorage . KB ) ;
394
396
}
397
+
395
398
return Math . Round ( freeSpace , 3 ) + " GB" ;
396
399
}
397
400
@@ -410,6 +413,7 @@ public static string DriveFormat(this string driveLetter)
410
413
continue ;
411
414
driveFormat = drive . DriveFormat ;
412
415
}
416
+
413
417
return driveFormat ;
414
418
}
415
419
@@ -428,6 +432,7 @@ public static DriveType DriveType(this string driveLetter)
428
432
continue ;
429
433
type = drive . DriveType ;
430
434
}
435
+
431
436
return type ;
432
437
}
433
438
@@ -539,5 +544,54 @@ public static double FileSizeInMB(this string filePath)
539
544
return Math . Round ( fileSizeInMB , 3 , MidpointRounding . AwayFromZero ) ;
540
545
}
541
546
547
+ /// <summary>
548
+ /// Joins string array into a single string separated via delimiter.
549
+ /// </summary>
550
+ /// <param name="texts">string array to join.</param>
551
+ /// <param name="delimiter">delimiter to join strings with (eg: , ).</param>
552
+ /// <returns>A Single string based on given array.</returns>
553
+ public static string Join ( this IEnumerable < string > texts , string delimiter )
554
+ {
555
+ var str = new StringBuilder ( ) ;
556
+ foreach ( var text in texts )
557
+ {
558
+ str . Append ( $ "{ text } { delimiter } ") ;
559
+ }
560
+
561
+ return str . ToString ( ) ;
562
+ }
563
+
564
+ /// <summary>
565
+ /// Reads the file content as array of bytes.
566
+ /// </summary>
567
+ /// <param name="filePath">Path of the file to read.</param>
568
+ /// <returns>Byte array of the given file content.</returns>
569
+ public static byte [ ] ReadBytesFromDisk ( this string filePath )
570
+ {
571
+ if ( filePath . IsEmpty ( ) )
572
+ throw new Exception ( "The given path is empty" ) ;
573
+
574
+ return File . ReadAllBytes ( filePath ) ;
575
+ }
576
+
577
+ /// <summary>
578
+ /// Reads the file content as array of bytes asynchronously.
579
+ /// </summary>
580
+ /// <param name="filePath">Path of the file to read.</param>
581
+ /// <returns>Byte array of the given file content.</returns>
582
+ public static async Task < byte [ ] > ReadBytesFromDiskAsync ( this string filePath )
583
+ {
584
+ if ( filePath . IsEmpty ( ) )
585
+ throw new Exception ( "The given path is empty" ) ;
586
+
587
+ byte [ ] outputFile ;
588
+ using ( var fs = new FileStream ( filePath , FileMode . Open , FileAccess . Read ) )
589
+ {
590
+ outputFile = new byte [ fs . Length ] ;
591
+ await fs . ReadAsync ( outputFile , 0 , ( int ) fs . Length ) ;
592
+ }
593
+
594
+ return outputFile ;
595
+ }
542
596
}
543
597
}
0 commit comments