@@ -138,72 +138,87 @@ await System.Threading.Tasks.Task.Run(() =>
138138
139139 private void btnAddFiles_Click ( object ? sender , System . EventArgs e )
140140 {
141+ if ( ! IsWindows )
142+ {
143+ AddFilesAlternativeMethod ( ) ;
144+ return ;
145+ }
146+
141147 try
142148 {
143- if ( IsWindows )
149+ using ( var dialog = new OpenFileDialog
144150 {
145- // Use Windows Forms dialog on Windows
146- using ( var dialog = new OpenFileDialog ( ) )
151+ Multiselect = true ,
152+ Title = "Select files" ,
153+ Filter = "All files (*.*)|*.*"
154+ } )
155+ {
156+ if ( dialog . ShowDialog ( ) == DialogResult . OK )
147157 {
148- dialog . Multiselect = true ;
149- dialog . Title = "Select files" ;
150- dialog . Filter = "All files (*.*)|*.*" ;
151- if ( dialog . ShowDialog ( ) == DialogResult . OK )
152- {
153- foreach ( string file in dialog . FileNames )
154- {
155- AddFileToList ( file ) ;
156- }
157- UpdateStatus ( $ "Added { dialog . FileNames . Length } files. Total: { listViewFiles . Items . Count } ") ;
158- }
158+ AddFilesToList ( dialog . FileNames ) ;
159159 }
160160 }
161- else
162- {
163- // Alternative method for non-Windows platforms
164- AddFilesAlternativeMethod ( ) ;
165- }
166161 }
167162 catch ( Exception ex )
168163 {
169- MessageBox . Show ( $ "Error adding files: { ex . Message } \n \n Tip: On non-Windows platforms, you can manually enter file paths or use the alternative method.",
170- "File Selection Error" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
171- UpdateStatus ( "Error occurred while adding files. See message for details." ) ;
164+ ShowError ( "Error adding files" , ex ) ;
172165 }
173166 }
174167
175- private void btnAddFolder_Click ( object ? sender , System . EventArgs e )
168+ private async void btnAddFolder_Click ( object ? sender , System . EventArgs e )
176169 {
170+ if ( ! IsWindows )
171+ {
172+ AddFolderAlternativeMethod ( ) ;
173+ return ;
174+ }
175+
177176 try
178177 {
179- if ( IsWindows )
178+ using ( var dialog = new FolderBrowserDialog
179+ {
180+ Description = "Select a folder to add all its files"
181+ } )
180182 {
181- // Use Windows Forms dialog on Windows
182- using ( var dialog = new FolderBrowserDialog ( ) )
183+ if ( dialog . ShowDialog ( ) == DialogResult . OK )
183184 {
184- dialog . Description = "Select a folder to add all its files" ;
185- if ( dialog . ShowDialog ( ) == DialogResult . OK )
186- {
187- var files = Directory . GetFiles ( dialog . SelectedPath , "*.*" , SearchOption . AllDirectories ) ;
188- foreach ( string file in files )
189- {
190- AddFileToList ( file ) ;
191- }
192- UpdateStatus ( $ "Added { files . Length } files from folder. Total: { listViewFiles . Items . Count } ") ;
193- }
185+ await AddFolderFilesAsync ( dialog . SelectedPath ) ;
194186 }
195187 }
196- else
197- {
198- // Alternative method for non-Windows platforms
199- AddFolderAlternativeMethod ( ) ;
200- }
201188 }
202189 catch ( Exception ex )
203190 {
204- MessageBox . Show ( $ "Error adding folder: { ex . Message } \n \n Tip: On non-Windows platforms, you can manually enter folder paths or use the alternative method.",
205- "Folder Selection Error" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
206- UpdateStatus ( "Error occurred while adding folder. See message for details." ) ;
191+ ShowError ( "Error adding folder" , ex ) ;
192+ }
193+ }
194+
195+ private async System . Threading . Tasks . Task AddFolderFilesAsync ( string path )
196+ {
197+ SetUIEnabled ( false ) ;
198+ UpdateStatus ( "Searching for files..." ) ;
199+
200+ try
201+ {
202+ var files = await System . Threading . Tasks . Task . Run ( ( ) =>
203+ Directory . GetFiles ( path , "*.*" , SearchOption . AllDirectories )
204+ ) ;
205+ AddFilesToList ( files ) ;
206+ }
207+ catch ( UnauthorizedAccessException ex )
208+ {
209+ ShowError ( "Access denied. You may not have permission to access all subdirectories." , ex ) ;
210+ }
211+ catch ( IOException ex )
212+ {
213+ ShowError ( "An I/O error occurred while accessing the folder." , ex ) ;
214+ }
215+ catch ( Exception ex )
216+ {
217+ ShowError ( "An unexpected error occurred while adding folder files." , ex ) ;
218+ }
219+ finally
220+ {
221+ SetUIEnabled ( true ) ;
207222 }
208223 }
209224
@@ -237,16 +252,11 @@ private void AddFilesAlternativeMethod()
237252 . Take ( 5 ) // Limit to first 5 files
238253 . ToArray ( ) ;
239254
240- foreach ( string file in files )
241- {
242- AddFileToList ( file ) ;
243- }
244-
245- UpdateStatus ( $ "Added { files . Length } test files from current directory. Total: { listViewFiles . Items . Count } ") ;
255+ AddFilesToList ( files ) ;
246256 }
247257 catch ( Exception ex )
248258 {
249- MessageBox . Show ( $ "Error adding test files: { ex . Message } ", "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
259+ ShowError ( "Error adding test files" , ex ) ;
250260 }
251261 }
252262 }
@@ -271,31 +281,36 @@ private void AddFolderAlternativeMethod()
271281 {
272282 var currentDir = Directory . GetCurrentDirectory ( ) ;
273283 var files = Directory . GetFiles ( currentDir , "*.*" , SearchOption . AllDirectories ) ;
274-
275- foreach ( string file in files )
276- {
277- AddFileToList ( file ) ;
278- }
279-
280- UpdateStatus ( $ "Added { files . Length } files from current directory and subdirectories. Total: { listViewFiles . Items . Count } ") ;
284+ AddFilesToList ( files ) ;
281285 }
282286 catch ( Exception ex )
283287 {
284- MessageBox . Show ( $ "Error adding files from folder: { ex . Message } ", "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
288+ ShowError ( "Error adding files from folder" , ex ) ;
285289 }
286290 }
287291 }
288292
289- private void AddFileToList ( string filePath )
293+ private void AddFilesToList ( string [ ] filePaths )
290294 {
291- // Check if the file is already in the list to avoid duplicates
292- if ( ! listViewFiles . Items . Cast < ListViewItem > ( ) . Any ( item => item . Text == filePath ) )
295+ var newItems = filePaths
296+ . Where ( filePath => ! listViewFiles . Items . Cast < ListViewItem > ( ) . Any ( item => item . Text == filePath ) )
297+ . Select ( filePath => new ListViewItem ( filePath ) )
298+ . ToArray ( ) ;
299+
300+ if ( newItems . Any ( ) )
293301 {
294- var item = new ListViewItem ( filePath ) ;
295- listViewFiles . Items . Add ( item ) ;
302+ listViewFiles . Items . AddRange ( newItems ) ;
303+ UpdateStatus ( $ "Added { newItems . Length } files. Total: { listViewFiles . Items . Count } " ) ;
296304 }
297305 }
298306
307+ private void ShowError ( string title , Exception ex )
308+ {
309+ MessageBox . Show ( $ "{ ex . Message } \n \n Tip: On non-Windows platforms, you can use the console version for more detailed error information.",
310+ title , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
311+ UpdateStatus ( $ "Error: { title } . See message for details.") ;
312+ }
313+
299314 private void UpdateStatus ( string text )
300315 {
301316 statusLabel . Text = text ;
0 commit comments