FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SabreTools.IO/SabreTools.IO.Extensions/IOExtensions.cs at main · SabreTools/SabreTools.IO · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
SabreTools
/
SabreTools.IO
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
2
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SabreTools.IO
/
SabreTools.IO.Extensions
/
IOExtensions.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
1207 lines (1063 loc) · 47.1 KB
Breadcrumbs
SabreTools.IO
/
SabreTools.IO.Extensions
/
IOExtensions.cs
Copy path
File metadata and controls
1207 lines (1063 loc) · 47.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
IO
;
using
System
.
Text
;
using
System
.
Text
.
RegularExpressions
;
using
SabreTools
.
Collections
.
Extensions
;
using
SabreTools
.
Text
.
Compare
;
namespace
SabreTools
.
IO
.
Extensions
{
/// <summary>
/// Methods around path operations
/// </summary>
public
static
class
IOExtensions
{
#region Directory
/// <summary>
/// Ensure the output directory is a proper format and can be created
/// </summary>
/// <param name="dir">Directory to check</param>
/// <param name="create">True if the directory should be created, false otherwise (default)</param>
/// <returns>Full path to the directory</returns>
public
static
string
EnsureDirectory
(
this
string
?
dir
,
bool
create
=
false
)
{
// If the output directory is invalid
if
(
string
.
IsNullOrEmpty
(
dir
)
)
dir
=
PathTool
.
GetRuntimeDirectory
(
)
;
// Get the full path for the output directory
dir
=
Path
.
GetFullPath
(
dir
!
.
Trim
(
'"'
)
)
;
// If we're creating the output folder, do so
if
(
create
&&
!
Directory
.
Exists
(
dir
)
)
Directory
.
CreateDirectory
(
dir
)
;
return
dir
;
}
/// <summary>
/// Retrieve a list of just directories from inputs
/// </summary>
/// <param name="inputs">List of strings representing directories and files</param>
/// <param name="appendParent">True if the parent name should be included in the ParentablePath, false otherwise (default)</param>
/// <returns>List of strings representing just directories from the inputs</returns>
public
static
List
<
ParentablePath
>
GetDirectoriesOnly
(
List
<
string
>
inputs
,
bool
appendParent
=
false
)
{
var
outputs
=
new
List
<
ParentablePath
>
(
)
;
for
(
int
i
=
0
;
i
<
inputs
.
Count
;
i
++
)
{
string
input
=
inputs
[
i
]
;
// If we have a null or empty path
if
(
string
.
IsNullOrEmpty
(
input
)
)
continue
;
// If we have a wildcard
string
pattern
=
"*"
;
if
(
input
.
Contains
(
"*"
)
||
input
.
Contains
(
"?"
)
)
{
pattern
=
Path
.
GetFileName
(
input
)
;
input
=
input
.
Substring
(
0
,
input
.
Length
-
pattern
.
Length
)
;
}
// Get the parent path in case of appending
string
parentPath
=
Path
.
GetFullPath
(
input
)
;
if
(
Directory
.
Exists
(
input
)
)
{
List
<
string
>
directories
=
input
.
GetDirectoriesOrdered
(
pattern
)
;
foreach
(
string
dir
in
directories
)
{
outputs
.
Add
(
new
ParentablePath
(
Path
.
GetFullPath
(
dir
)
,
appendParent
?
parentPath
:
string
.
Empty
)
)
;
}
}
}
return
outputs
;
}
/// <summary>
/// Retrieve a list of directories from a directory recursively in proper order
/// </summary>
/// <param name="dir">Directory to parse</param>
/// <param name="pattern">Optional pattern to search for directory names</param>
/// <returns>List with all new files</returns>
public
static
List
<
string
>
GetDirectoriesOrdered
(
this
string
dir
,
string
pattern
=
"*"
)
=>
GetDirectoriesOrderedHelper
(
dir
,
[
]
,
pattern
)
;
/// <summary>
/// Retrieve a list of directories from a directory recursively in proper order
/// </summary>
/// <param name="dir">Directory to parse</param>
/// <param name="infiles">List representing existing files</param>
/// <param name="pattern">Optional pattern to search for directory names</param>
/// <returns>List with all new files</returns>
private
static
List
<
string
>
GetDirectoriesOrderedHelper
(
string
dir
,
List
<
string
>
infiles
,
string
pattern
)
{
// Take care of the files in the top directory
List
<
string
>
toadd
=
[
..
dir
.
SafeEnumerateDirectories
(
pattern
,
SearchOption
.
TopDirectoryOnly
)
]
;
toadd
.
Sort
(
new
NaturalComparer
(
)
)
;
infiles
.
AddRange
(
toadd
)
;
// Then recurse through and add from the directories
foreach
(
string
subDir
in
toadd
)
{
infiles
=
GetDirectoriesOrderedHelper
(
subDir
,
infiles
,
pattern
)
;
}
// Return the new list
return
infiles
;
}
/// <summary>
/// Get all empty folders within a root folder
/// </summary>
/// <param name="root">Root directory to parse</param>
/// <returns>IEumerable containing all directories that are empty, an empty enumerable if the root is empty, null otherwise</returns>
public
static
List
<
string
>
?
ListEmpty
(
this
string
?
root
)
{
// Check null or empty first
if
(
root
is
null
)
return
null
;
// Then, check if the root exists
if
(
!
Directory
.
Exists
(
root
)
)
return
null
;
// Otherwise, get the complete list
var
empty
=
new
List
<
string
>
(
)
;
foreach
(
var
dir
in
SafeGetDirectories
(
root
,
"*"
,
SearchOption
.
AllDirectories
)
)
{
if
(
SafeGetFiles
(
dir
)
.
Length
==
0
)
empty
.
Add
(
dir
)
;
}
return
empty
;
}
#endregion
#region File
/// <summary>
/// Concatenate all files in the order provided, if possible
/// </summary>
/// <param name="paths">List of paths to combine</param>
/// <param name="output">Path to the output file</param>
/// <returns>True if the files were concatenated successfully, false otherwise</returns>
public
static
bool
Concatenate
(
List
<
string
>
paths
,
string
output
)
{
// If the path list is empty
if
(
paths
.
Count
==
0
)
return
false
;
// If the output filename is invalid
if
(
string
.
IsNullOrEmpty
(
output
)
)
return
false
;
try
{
// Try to build the new output file
using
var
ofs
=
File
.
Open
(
output
,
FileMode
.
Create
,
FileAccess
.
Write
,
FileShare
.
None
)
;
for
(
int
i
=
0
;
i
<
paths
.
Count
;
i
++
)
{
// Get the next file
string
next
=
paths
[
i
]
;
if
(
!
File
.
Exists
(
next
)
)
break
;
// Copy the next input to the output
using
var
ifs
=
File
.
Open
(
next
,
FileMode
.
Open
,
FileAccess
.
Read
,
FileShare
.
ReadWrite
)
;
ifs
.
BlockCopy
(
ofs
,
blockSize
:
3
*
1024
*
1024
)
;
}
return
true
;
}
catch
{
// Absorb the exception right now
return
false
;
}
}
/// <summary>
/// Determines a text file's encoding by analyzing its byte order mark (BOM).
/// Defaults to ASCII when detection of the text file's endianness fails.
/// </summary>
/// <param name="filename">The text file to analyze.</param>
/// <returns>The detected encoding.</returns>
/// <link>http://stackoverflow.com/questions/3825390/effective-way-to-find-any-files-encoding</link>
public
static
Encoding
GetEncoding
(
this
string
filename
)
{
if
(
string
.
IsNullOrEmpty
(
filename
)
)
return
Encoding
.
Default
;
if
(
!
File
.
Exists
(
filename
)
)
return
Encoding
.
Default
;
// Try to open the file
try
{
var
file
=
File
.
Open
(
filename
,
FileMode
.
Open
,
FileAccess
.
Read
,
FileShare
.
ReadWrite
)
;
if
(
file
is
null
)
return
Encoding
.
Default
;
// Read the BOM
var
bom
=
new
byte
[
4
]
;
int
read
=
file
.
Read
(
bom
,
0
,
4
)
;
file
.
Dispose
(
)
;
// Disable warning about UTF7 usage
#if
NET5_0_OR_GREATER
#pragma warning disable
SYSLIB0001
#endif
// Analyze the BOM
if
(
bom
[
0
]
==
0x2b
&&
bom
[
1
]
==
0x2f
&&
bom
[
2
]
==
0x76
)
return
Encoding
.
UTF7
;
if
(
bom
[
0
]
==
0xef
&&
bom
[
1
]
==
0xbb
&&
bom
[
2
]
==
0xbf
)
return
Encoding
.
UTF8
;
if
(
bom
[
0
]
==
0xff
&&
bom
[
1
]
==
0xfe
)
return
Encoding
.
Unicode
;
//UTF-16LE
if
(
bom
[
0
]
==
0xfe
&&
bom
[
1
]
==
0xff
)
return
Encoding
.
BigEndianUnicode
;
//UTF-16BE
if
(
bom
[
0
]
==
0
&&
bom
[
1
]
==
0
&&
bom
[
2
]
==
0xfe
&&
bom
[
3
]
==
0xff
)
return
Encoding
.
UTF32
;
return
Encoding
.
Default
;
// Restore warning about UTF7 usage
#if
NET5_0_OR_GREATER
#pragma warning restore
SYSLIB0001
#endif
}
catch
{
return
Encoding
.
Default
;
}
}
/// <summary>
/// Retrieve a list of just files from inputs
/// </summary>
/// <param name="inputs">List of strings representing directories and files</param>
/// <param name="appendParent">True if the parent name should be be included in the ParentablePath, false otherwise (default)</param>
/// <returns>List of strings representing just files from the inputs</returns>
public
static
List
<
ParentablePath
>
GetFilesOnly
(
List
<
string
>
inputs
,
bool
appendParent
=
false
)
{
var
outputs
=
new
List
<
ParentablePath
>
(
)
;
for
(
int
i
=
0
;
i
<
inputs
.
Count
;
i
++
)
{
string
input
=
inputs
[
i
]
.
Trim
(
'"'
)
;
// If we have a null or empty path
if
(
string
.
IsNullOrEmpty
(
input
)
)
continue
;
// If we have a wildcard
string
pattern
=
"*"
;
if
(
input
.
Contains
(
"*"
)
||
input
.
Contains
(
"?"
)
)
{
pattern
=
Path
.
GetFileName
(
input
)
;
input
=
input
.
Substring
(
0
,
input
.
Length
-
pattern
.
Length
)
;
}
// Get the parent path in case of appending
string
parentPath
=
Path
.
GetFullPath
(
input
)
;
if
(
Directory
.
Exists
(
input
)
)
{
List
<
string
>
files
=
input
.
GetFilesOrdered
(
pattern
)
;
foreach
(
string
file
in
files
)
{
outputs
.
Add
(
new
ParentablePath
(
Path
.
GetFullPath
(
file
)
,
appendParent
?
parentPath
:
string
.
Empty
)
)
;
}
}
else
if
(
File
.
Exists
(
input
)
)
{
outputs
.
Add
(
new
ParentablePath
(
Path
.
GetFullPath
(
input
)
,
appendParent
?
parentPath
:
string
.
Empty
)
)
;
}
}
return
outputs
;
}
/// <summary>
/// Retrieve a list of files from a directory recursively in proper order
/// </summary>
/// <param name="dir">Directory to parse</param>
/// <param name="pattern">Optional pattern to search for directory names</param>
/// <returns>List with all new files</returns>
public
static
List
<
string
>
GetFilesOrdered
(
this
string
dir
,
string
pattern
=
"*"
)
=>
GetFilesOrderedHelper
(
dir
,
[
]
,
pattern
)
;
/// <summary>
/// Retrieve a list of files from a directory recursively in proper order
/// </summary>
/// <param name="dir">Directory to parse</param>
/// <param name="infiles">List representing existing files</param>
/// <param name="pattern">Optional pattern to search for directory names</param>
/// <returns>List with all new files</returns>
private
static
List
<
string
>
GetFilesOrderedHelper
(
string
dir
,
List
<
string
>
infiles
,
string
pattern
)
{
// Take care of the files in the top directory
List
<
string
>
toadd
=
[
..
dir
.
SafeEnumerateFiles
(
pattern
,
SearchOption
.
TopDirectoryOnly
)
]
;
toadd
.
Sort
(
new
NaturalComparer
(
)
)
;
infiles
.
AddRange
(
toadd
)
;
// Then recurse through and add from the directories
List
<
string
>
subDirs
=
[
..
dir
.
SafeEnumerateDirectories
(
pattern
,
SearchOption
.
TopDirectoryOnly
)
]
;
subDirs
.
Sort
(
new
NaturalComparer
(
)
)
;
foreach
(
string
subdir
in
subDirs
)
{
infiles
=
GetFilesOrderedHelper
(
subdir
,
infiles
,
pattern
)
;
}
// Return the new list
return
infiles
;
}
/// <summary>
/// Helper to get the filesize from a path
/// </summary>
/// <returns>Size of the file path, -1 on error</returns>
public
static
long
GetFileSize
(
this
string
?
filename
)
{
// Invalid filenames are ignored
if
(
string
.
IsNullOrEmpty
(
filename
)
)
return
-
1
;
// Non-file paths are ignored
if
(
!
File
.
Exists
(
filename
)
)
return
-
1
;
try
{
return
new
FileInfo
(
filename
)
.
Length
;
}
catch
{
// Ignore errors
return
-
1
;
}
}
/// <summary>
/// Get the extension from the path, if possible
/// </summary>
/// <param name="path">Path to get extension from</param>
/// <returns>Extension, if possible</returns>
public
static
string
?
GetNormalizedExtension
(
this
string
?
path
)
{
// Check null or empty first
if
(
string
.
IsNullOrEmpty
(
path
)
)
return
null
;
// Get the extension from the path, if possible
string
?
ext
=
Path
.
GetExtension
(
path
)
?
.
ToLowerInvariant
(
)
;
// Check if the extension is null or empty
if
(
string
.
IsNullOrEmpty
(
ext
)
)
return
null
;
// Make sure that extensions are valid
ext
=
ext
!
.
TrimStart
(
'.'
)
;
return
ext
;
}
/// <inheritdoc cref="NormalizeFilePath(string?, bool)"/>
public
static
string
NormalizeFilePath
(
this
string
?
path
)
=>
path
.
NormalizeFilePath
(
fullPath
:
true
)
;
/// <summary>
/// Normalize a file path
/// </summary>
/// <param name="path">Path value to normalize</param>
/// <param name="fullPath">Indicates if the full path is used</param>
/// <returns>The normalized path on success, an empty path if null, or the original path on error</returns>
/// <remarks>
/// This method performs the following steps:
/// - Remove quotes and angle brackets from the start and end
/// - Replaces invalid path characters with '_'
/// - Remove spaces before and after directory separators
/// </remarks>
public
static
string
NormalizeFilePath
(
this
string
?
path
,
bool
fullPath
)
{
try
{
// If we have an invalid path
if
(
string
.
IsNullOrEmpty
(
path
)
)
return
string
.
Empty
;
// Remove quotes and angle brackets from path
path
=
path
!
.
Trim
(
'
\"
'
)
;
path
=
path
!
.
Trim
(
'<'
)
;
path
=
path
!
.
Trim
(
'>'
)
;
// Remove invalid path characters
foreach
(
char
c
in
Path
.
GetInvalidPathChars
(
)
)
{
path
=
path
.
Replace
(
c
,
'_'
)
;
}
// Try getting the combined path and returning that directly
string
usablePath
=
fullPath
?
Path
.
GetFullPath
(
path
)
:
path
;
var
fullDirectory
=
Path
.
GetDirectoryName
(
usablePath
)
?
.
Trim
(
)
;
string
fullFile
=
Path
.
GetFileName
(
usablePath
)
.
Trim
(
)
;
// Remove invalid filename characters
foreach
(
char
c
in
Path
.
GetInvalidFileNameChars
(
)
)
{
fullFile
=
fullFile
.
Replace
(
c
,
'_'
)
;
}
// Rebuild the path, if necessary
if
(
!
string
.
IsNullOrEmpty
(
fullDirectory
)
)
fullFile
=
Path
.
Combine
(
fullDirectory
,
fullFile
)
;
// Remove spaces before and after separators
return
Regex
.
Replace
(
fullFile
,
@"\s*([\\|/])\s*"
,
@"$1"
)
;
}
catch
{
}
return
path
??
string
.
Empty
;
}
/// <summary>
/// Resolve a file path that may be absolute, relative,
/// within the runtime directory, or contained within a PATH directory.
/// </summary>
/// <param name="path">Raw value from the user's options</param>
/// <returns>The absolute path of the located files, or null on failure</returns>
public
static
string
?
ResolvePath
(
this
string
?
path
)
{
// Invalid paths always return null
if
(
string
.
IsNullOrEmpty
(
path
)
)
return
null
;
// If the configured path starts with a home character
if
(
path
!
.
StartsWith
(
"~/"
)
||
path
.
StartsWith
(
"~
\\
"
)
)
{
string
homeDirectory
=
PathTool
.
GetHomeDirectory
(
)
;
path
=
path
.
Substring
(
2
)
;
path
=
Path
.
Combine
(
homeDirectory
,
path
)
;
return
File
.
Exists
(
path
)
?
Path
.
GetFullPath
(
path
)
:
null
;
}
// Explicit location (absolute or relative path)
if
(
path
.
Contains
(
"/"
)
||
path
.
Contains
(
"
\\
"
)
)
return
File
.
Exists
(
path
)
?
Path
.
GetFullPath
(
path
)
:
null
;
// Check the runtime directory if no directory path is provided
string
runtimeDir
=
PathTool
.
GetRuntimeDirectory
(
)
;
if
(
!
string
.
IsNullOrEmpty
(
runtimeDir
)
)
{
string
candidate
=
Path
.
Combine
(
runtimeDir
,
path
)
;
if
(
File
.
Exists
(
candidate
)
)
return
candidate
;
}
// Attempt to get the PATH variable for searching
string
?
pathEnv
=
Environment
.
GetEnvironmentVariable
(
"PATH"
)
;
if
(
string
.
IsNullOrEmpty
(
pathEnv
)
)
return
null
;
// Loop through all entries in PATH
var
pathParts
=
pathEnv
!
.
Split
(
Path
.
PathSeparator
)
;
foreach
(
string
dir
in
pathParts
)
{
if
(
string
.
IsNullOrEmpty
(
dir
)
)
continue
;
string
candidate
=
Path
.
Combine
(
dir
,
path
)
;
if
(
File
.
Exists
(
candidate
)
)
return
candidate
;
}
// All options failed
return
null
;
}
#endregion
#region Safe Directory Enumeration
/// <inheritdoc cref="Directory.GetDirectories(string)"/>
/// <remarks>Returns an empty enumerable on any exception</remarks>
public
static
string
[
]
SafeGetDirectories
(
this
string
path
)
{
try
{
return
Directory
.
GetDirectories
(
path
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.GetDirectories(string, string)"/>
/// <remarks>Returns an empty enumerable on any exception</remarks>
public
static
string
[
]
SafeGetDirectories
(
this
string
path
,
string
searchPattern
)
{
try
{
return
Directory
.
GetDirectories
(
path
,
searchPattern
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.GetDirectories(string, string, SearchOption)"/>
/// <remarks>Returns an empty enumerable on any exception</remarks>
public
static
string
[
]
SafeGetDirectories
(
this
string
path
,
string
searchPattern
,
SearchOption
searchOption
)
{
try
{
return
Directory
.
GetDirectories
(
path
,
searchPattern
,
searchOption
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.GetFiles(string)"/>
/// <remarks>Returns an empty enumerable on any exception</remarks>
public
static
string
[
]
SafeGetFiles
(
this
string
path
)
{
try
{
return
Directory
.
GetFiles
(
path
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.GetFiles(string, string)"/>
/// <remarks>Returns an empty enumerable on any exception</remarks>
public
static
string
[
]
SafeGetFiles
(
this
string
path
,
string
searchPattern
)
{
try
{
return
Directory
.
GetFiles
(
path
,
searchPattern
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.GetFiles(string, string, SearchOption)"/>
/// <remarks>Returns an empty enumerable on any exception</remarks>
public
static
string
[
]
SafeGetFiles
(
this
string
path
,
string
searchPattern
,
SearchOption
searchOption
)
{
try
{
return
Directory
.
GetFiles
(
path
,
searchPattern
,
searchOption
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.GetFileSystemEntries(string)"/>
/// <remarks>Returns an empty enumerable on any exception</remarks>
public
static
string
[
]
SafeGetFileSystemEntries
(
this
string
path
)
{
try
{
return
Directory
.
GetFileSystemEntries
(
path
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.GetDirectories(string, string)"/>
/// <remarks>Returns an empty enumerable on any exception</remarks>
public
static
string
[
]
SafeGetFileSystemEntries
(
this
string
path
,
string
searchPattern
)
{
try
{
return
Directory
.
GetFileSystemEntries
(
path
,
searchPattern
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.GetDirectories(string, string, SearchOption)"/>
/// <remarks>Returns an empty enumerable on any exception</remarks>
public
static
IEnumerable
<
string
>
SafeGetFileSystemEntries
(
this
string
path
,
string
searchPattern
,
SearchOption
searchOption
)
{
try
{
var
enumerable
=
Directory
.
GetFileSystemEntries
(
path
,
searchPattern
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
#if
NET20
||
NET35
/// <inheritdoc cref="Directory.GetDirectories(string)"/>
/// <remarks>Calls <see cref="SafeGetDirectories(string)"/> implementation</remarks>
public
static
IEnumerable
<
string
>
SafeEnumerateDirectories
(
this
string
path
)
=>
path
.
SafeGetDirectories
(
)
;
/// <inheritdoc cref="Directory.GetDirectories(string, string)"/>
/// <remarks>Calls <see cref="SafeGetDirectories(string, string)"/> implementation</remarks>
public
static
IEnumerable
<
string
>
SafeEnumerateDirectories
(
this
string
path
,
string
searchPattern
)
=>
path
.
SafeGetDirectories
(
searchPattern
)
;
/// <inheritdoc cref="Directory.GetDirectories(string, string, SearchOption)"/>
/// <remarks>Calls <see cref="SafeGetDirectories(string, string, SearchOption)"/> implementation</remarks>
public
static
IEnumerable
<
string
>
SafeEnumerateDirectories
(
this
string
path
,
string
searchPattern
,
SearchOption
searchOption
)
=>
path
.
SafeGetDirectories
(
searchPattern
,
searchOption
)
;
/// <inheritdoc cref="Directory.GetFiles(string)"/>
/// <remarks>Calls <see cref="SafeGetFiles(string)"/> implementation</remarks>
public
static
IEnumerable
<
string
>
SafeEnumerateFiles
(
this
string
path
)
=>
path
.
SafeGetFiles
(
)
;
/// <inheritdoc cref="Directory.GetFiles(string, string)"/>
/// <remarks>Calls <see cref="SafeGetFiles(string, string)"/> implementation</remarks>
public
static
IEnumerable
<
string
>
SafeEnumerateFiles
(
this
string
path
,
string
searchPattern
)
=>
path
.
SafeGetFiles
(
searchPattern
)
;
/// <inheritdoc cref="Directory.GetFiles(string, string, SearchOption)"/>
/// <remarks>Calls <see cref="SafeGetFiles(string, string, SearchOption)"/> implementation</remarks>
public
static
IEnumerable
<
string
>
SafeEnumerateFiles
(
this
string
path
,
string
searchPattern
,
SearchOption
searchOption
)
=>
path
.
SafeGetFiles
(
searchPattern
,
searchOption
)
;
/// <inheritdoc cref="Directory.GetFileSystemEntries(string)"/>
/// <remarks>Calls <see cref="SafeGetFileSystemEntries(string)"/> implementation</remarks>
public
static
IEnumerable
<
string
>
SafeEnumerateFileSystemEntries
(
this
string
path
)
=>
path
.
SafeGetFileSystemEntries
(
)
;
/// <inheritdoc cref="Directory.GetFileSystemEntries(string, string)"/>
/// <remarks>Calls <see cref="SafeGetFileSystemEntries(string, string)"/> implementation</remarks>
public
static
IEnumerable
<
string
>
SafeEnumerateFileSystemEntries
(
this
string
path
,
string
searchPattern
)
=>
path
.
SafeGetFileSystemEntries
(
searchPattern
)
;
/// <inheritdoc cref="Directory.GetFileSystemEntries(string, string)"/>
/// <remarks>Calls <see cref="SafeGetFileSystemEntries(string, string, SearchOption)"/> implementation</remarks>
public
static
IEnumerable
<
string
>
SafeEnumerateFileSystemEntries
(
this
string
path
,
string
searchPattern
,
SearchOption
searchOption
)
=>
path
.
SafeGetFileSystemEntries
(
searchPattern
,
searchOption
)
;
#elif
NET40_OR_GREATER
||
NETSTANDARD2_0
/// <inheritdoc cref="Directory.EnumerateDirectories(string)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateDirectories
(
this
string
path
)
{
try
{
var
enumerable
=
Directory
.
EnumerateDirectories
(
path
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateDirectories(string, string)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateDirectories
(
this
string
path
,
string
searchPattern
)
{
try
{
var
enumerable
=
Directory
.
EnumerateDirectories
(
path
,
searchPattern
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateDirectories(string, string, SearchOption)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateDirectories
(
this
string
path
,
string
searchPattern
,
SearchOption
searchOption
)
{
try
{
var
enumerable
=
Directory
.
EnumerateDirectories
(
path
,
searchPattern
,
searchOption
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateFiles(string)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateFiles
(
this
string
path
)
{
try
{
var
enumerable
=
Directory
.
EnumerateFiles
(
path
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateFiles(string, string)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateFiles
(
this
string
path
,
string
searchPattern
)
{
try
{
var
enumerable
=
Directory
.
EnumerateFiles
(
path
,
searchPattern
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateFiles(string, string, SearchOption)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateFiles
(
this
string
path
,
string
searchPattern
,
SearchOption
searchOption
)
{
try
{
var
enumerable
=
Directory
.
EnumerateFiles
(
path
,
searchPattern
,
searchOption
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateFileSystemEntries(string)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateFileSystemEntries
(
this
string
path
)
{
try
{
var
enumerable
=
Directory
.
EnumerateFileSystemEntries
(
path
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateFileSystemEntries(string, string)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateFileSystemEntries
(
this
string
path
,
string
searchPattern
)
{
try
{
var
enumerable
=
Directory
.
EnumerateFileSystemEntries
(
path
,
searchPattern
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateFileSystemEntries(string, string, SearchOption)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateFileSystemEntries
(
this
string
path
,
string
searchPattern
,
SearchOption
searchOption
)
{
try
{
var
enumerable
=
Directory
.
EnumerateFileSystemEntries
(
path
,
searchPattern
,
searchOption
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
#else
/// <inheritdoc cref="Directory.EnumerateDirectories(string)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateDirectories
(
this
string
path
)
{
try
{
string
searchPattern
=
"*"
;
SearchOption
searchOption
=
SearchOption
.
TopDirectoryOnly
;
var
enumerationOptions
=
FromSearchOption
(
searchOption
)
;
enumerationOptions
.
IgnoreInaccessible
=
true
;
var
enumerable
=
Directory
.
EnumerateDirectories
(
path
,
searchPattern
,
enumerationOptions
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateDirectories(string, string)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateDirectories
(
this
string
path
,
string
searchPattern
)
{
try
{
SearchOption
searchOption
=
SearchOption
.
TopDirectoryOnly
;
var
enumerationOptions
=
FromSearchOption
(
searchOption
)
;
enumerationOptions
.
IgnoreInaccessible
=
true
;
var
enumerable
=
Directory
.
EnumerateDirectories
(
path
,
searchPattern
,
enumerationOptions
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateDirectories(string, string, SearchOption)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateDirectories
(
this
string
path
,
string
searchPattern
,
SearchOption
searchOption
)
{
try
{
var
enumerationOptions
=
FromSearchOption
(
searchOption
)
;
enumerationOptions
.
IgnoreInaccessible
=
true
;
var
enumerable
=
Directory
.
EnumerateDirectories
(
path
,
searchPattern
,
enumerationOptions
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateFiles(string)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateFiles
(
this
string
path
)
{
try
{
string
searchPattern
=
"*"
;
SearchOption
searchOption
=
SearchOption
.
TopDirectoryOnly
;
var
enumerationOptions
=
FromSearchOption
(
searchOption
)
;
enumerationOptions
.
IgnoreInaccessible
=
true
;
var
enumerable
=
Directory
.
EnumerateFiles
(
path
,
searchPattern
,
enumerationOptions
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateFiles(string, string)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateFiles
(
this
string
path
,
string
searchPattern
)
{
try
{
SearchOption
searchOption
=
SearchOption
.
TopDirectoryOnly
;
var
enumerationOptions
=
FromSearchOption
(
searchOption
)
;
enumerationOptions
.
IgnoreInaccessible
=
true
;
var
enumerable
=
Directory
.
EnumerateFiles
(
path
,
searchPattern
,
enumerationOptions
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateFiles(string, string, SearchOption)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateFiles
(
this
string
path
,
string
searchPattern
,
SearchOption
searchOption
)
{
try
{
var
enumerationOptions
=
FromSearchOption
(
searchOption
)
;
enumerationOptions
.
IgnoreInaccessible
=
true
;
var
enumerable
=
Directory
.
EnumerateFiles
(
path
,
searchPattern
,
enumerationOptions
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateFileSystemEntries(string)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateFileSystemEntries
(
this
string
path
)
{
try
{
string
searchPattern
=
"*"
;
SearchOption
searchOption
=
SearchOption
.
TopDirectoryOnly
;
var
enumerationOptions
=
FromSearchOption
(
searchOption
)
;
enumerationOptions
.
IgnoreInaccessible
=
true
;
var
enumerable
=
Directory
.
EnumerateFileSystemEntries
(
path
,
searchPattern
,
enumerationOptions
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateFileSystemEntries(string, string)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateFileSystemEntries
(
this
string
path
,
string
searchPattern
)
{
try
{
SearchOption
searchOption
=
SearchOption
.
TopDirectoryOnly
;
var
enumerationOptions
=
FromSearchOption
(
searchOption
)
;
enumerationOptions
.
IgnoreInaccessible
=
true
;
var
enumerable
=
Directory
.
EnumerateFileSystemEntries
(
path
,
searchPattern
,
enumerationOptions
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <inheritdoc cref="Directory.EnumerateFileSystemEntries(string, string, SearchOption)"/>
public
static
IEnumerable
<
string
>
SafeEnumerateFileSystemEntries
(
this
string
path
,
string
searchPattern
,
SearchOption
searchOption
)
{
try
{
var
enumerationOptions
=
FromSearchOption
(
searchOption
)
;
enumerationOptions
.
IgnoreInaccessible
=
true
;
var
enumerable
=
Directory
.
EnumerateFileSystemEntries
(
path
,
searchPattern
,
enumerationOptions
)
;
return
enumerable
.
SafeEnumerate
(
)
;
}
catch
{
return
[
]
;
}
}
/// <summary>Initializes a new instance of the <see cref="EnumerationOptions" /> class with the recommended default options.</summary>
/// <see href="https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/IO/EnumerationOptions.cs#L42"</remarks>
private
static
EnumerationOptions
FromSearchOption
(
SearchOption
searchOption
)
{
if
(
(
searchOption
!=
SearchOption
.
TopDirectoryOnly
)
&&
(
searchOption
!=
SearchOption
.
AllDirectories
)
)
throw
new
ArgumentOutOfRangeException
(
nameof
(
searchOption
)
)
;
return
searchOption
==
SearchOption
.
AllDirectories
?
new
EnumerationOptions
{
RecurseSubdirectories
=
true
,
MatchType
=
MatchType
.
Win32
,
AttributesToSkip
=
0
,
IgnoreInaccessible
=
false
}
:
new
EnumerationOptions
{
MatchType
=
MatchType
.
Win32
,
AttributesToSkip
=
0
,
IgnoreInaccessible
=
false
}
;
}
#endif
#endregion
#region Unix
/// <summary>
/// Floppy media sizes in bytes for the standard 5.25" and 3.5" PC formats
/// (360 KB, 720 KB, 1.2 MB, 1.44 MB, 2.88 MB). A removable SCSI disk reporting one of
/// these exact capacities is a USB floppy with media inserted; no other removable
/// storage uses these sizes, so this doubles as the floppy identity check.
/// </summary>
#if
NET20
private
static
readonly
List
<
long
>
_unixFloppyMediaSizes
=
#else
private
static
readonly
HashSet
<
long
>
_unixFloppyMediaSizes
=
#endif
[
368640
,
// 360 KB (5.25" DD)
737280
,
// 720 KB (3.5" DD)
1228800
,
// 1.2 MB (5.25" HD)
1474560
,
// 1.44 MB (3.5" HD)
2949120
,
// 2.88 MB (3.5" ED)
]
;
/// <summary>
/// Enumerate Linux floppy block devices under a directory by matching the kernel
/// convention "fd" followed by one or more digits (fd0, fd1, ...). This deliberately
/// rejects the "/dev/fd" symlink (no trailing digits) and format-specific nodes such
/// as "fd0u1440" (embedded non-digit characters), which are not whole-device targets.
/// </summary>
/// <param name="devRoot">Root directory to scan (typically "/dev")</param>
/// <returns>Device paths, or an empty list when the directory is unreadable</returns>
public
static
List
<
string
>
EnumerateUnixFloppyBlockPaths
(
string
?
devRoot
=
"/dev"
)
{
// If the device root is invalid
if
(
string
.
IsNullOrEmpty
(
devRoot
)
||
!
Directory
.
Exists
(
devRoot
)
)
return
[
]
;
// Attempt to retrieve all matching devices
string
[
]
entries
=
SafeGetFiles
(
devRoot
!
,
"fd*"
)
;
// Return only valid devices
var
result
=
new
List
<
string
>
(
)
;
foreach
(
var
path
in
entries
)
{
if
(
HasDeviceIndexSuffix
(
Path
.
GetFileName
(
path
)
,
"fd"
)
)
result
.
Add
(
path
)
;
}
return
result
;
}
/// <summary>
/// Enumerate Linux optical block devices under a directory by matching
/// the kernel convention "sr" followed by one or more digits (sr0, sr1, ...).
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL