1414using System . Diagnostics ;
1515using System . IO ;
1616using System . Linq ;
17+ using System . Threading . Tasks ;
1718
1819namespace FileHandler ;
1920
@@ -31,75 +32,58 @@ public static class FileHandleCut
3132 /// <param name="overwrite">Is overwrite allowed</param>
3233 /// <returns>Status if we encountered any problems</returns>
3334 /// <exception cref="FileHandlerException">No Correct Path was provided</exception>
34- public static bool CutFiles ( string source , string target , bool overwrite )
35+ public static async Task < bool > CutFiles ( string source , string target , bool overwrite )
3536 {
3637 FileHandlerProcessing . ValidatePaths ( source , target ) ;
3738
38- //if nothing exists we can return anyways
3939 if ( ! Directory . Exists ( source ) )
40- {
4140 return false ;
42- }
4341
4442 var check = true ;
4543 var dir = new DirectoryInfo ( source ) ;
4644 var dirs = dir . GetDirectories ( ) ;
4745 var files = dir . GetFiles ( ) ;
4846
49- //Give the User Optional Infos about the Amount we Copy
50- var lstFiles = ( from file in files
51- select file . Name ) . ToList ( ) ;
47+ // Inform user
48+ var lstFiles = files . Select ( f => f . Name ) . ToList ( ) ;
49+ FileHandlerRegister . SendOverview ? . Invoke ( nameof ( CutFiles ) ,
50+ new FileItems { Elements = new List < string > ( lstFiles ) , Message = FileHandlerResources . InformationFileDeletion } ) ;
5251
53- var itm = new FileItems
52+ // Move files
53+ foreach ( var file in files )
5454 {
55- Elements = new List < string > ( lstFiles ) , Message = FileHandlerResources . InformationFileDeletion
56- } ;
57-
58- FileHandlerRegister . SendOverview ? . Invoke ( nameof ( CutFiles ) , itm ) ;
59-
60- //do the actual work
61- if ( files . Length > 0 )
62- {
63- if ( ! Directory . Exists ( target ) )
55+ var tempPath = Path . Combine ( target , file . Name ) ;
56+ try
6457 {
65- _ = Directory . CreateDirectory ( target ) ;
66- }
58+ if ( ! Directory . Exists ( target ) )
59+ _ = Directory . CreateDirectory ( target ) ;
6760
68- foreach ( var file in files )
61+ await Task . Run ( ( ) => file . MoveTo ( tempPath , overwrite ) ) . ConfigureAwait ( false ) ;
62+ FileHandlerRegister . SendStatus ? . Invoke ( nameof ( CutFiles ) , file . Name ) ;
63+ }
64+ catch ( Exception ex ) when ( ex is UnauthorizedAccessException or ArgumentException or IOException
65+ or NotSupportedException )
6966 {
70- var tempPath = Path . Combine ( target , file . Name ) ;
71-
72- try
73- {
74- file . MoveTo ( tempPath , overwrite ) ;
75-
76- FileHandlerRegister . SendStatus ? . Invoke ( nameof ( CutFiles ) , file . Name ) ;
77- }
78- catch ( Exception ex ) when ( ex is UnauthorizedAccessException or ArgumentException or IOException
79- or NotSupportedException )
80- {
81- FileHandlerRegister . AddError ( nameof ( CutFiles ) , file . Name , ex ) ;
82- Trace . WriteLine ( ex ) ;
83- check = false ;
84- }
67+ FileHandlerRegister . AddError ( nameof ( CutFiles ) , file . Name , ex ) ;
68+ Trace . WriteLine ( ex ) ;
69+ check = false ;
8570 }
8671 }
8772
73+ // Recurse subdirectories
8874 foreach ( var subDir in dirs )
8975 {
9076 var tempPath = Path . Combine ( target , subDir . Name ) ;
9177
9278 if ( ! Directory . Exists ( target ) )
93- {
9479 _ = Directory . CreateDirectory ( target ) ;
95- }
9680
9781 if ( Directory . Exists ( tempPath ) )
98- {
9982 continue ;
100- }
10183
102- _ = CutFiles ( subDir . FullName , tempPath , overwrite ) ;
84+ var subCheck = await CutFiles ( subDir . FullName , tempPath , overwrite ) . ConfigureAwait ( false ) ;
85+ if ( ! subCheck )
86+ check = false ;
10387 }
10488
10589 return check ;
0 commit comments