@@ -290,75 +290,78 @@ private void OnConvert(object sender, EventArgs e)
290290 foreach ( ListViewItem item in lstResults . CheckedItems )
291291 {
292292 string charset = item . SubItems [ RESULTS_COLUMN_CHARSET ] . Text ;
293- if ( charset . EndsWith ( "-bom" ) )
294- charset = charset . Replace ( "-bom" , "" ) ;
295293 if ( charset == "(Unknown)" )
296294 continue ;
295+
296+ if ( charset . EndsWith ( "-bom" ) )
297+ charset = charset . Replace ( "-bom" , "" ) ;
298+
297299 string fileName = item . SubItems [ RESULTS_COLUMN_FILE_NAME ] . Text ;
298300 string directory = item . SubItems [ RESULTS_COLUMN_DIRECTORY ] . Text ;
299301 string filePath = Path . Combine ( directory , fileName ) ;
300302
301- FileAttributes attributes = File . GetAttributes ( filePath ) ;
302- if ( ( attributes & FileAttributes . ReadOnly ) == FileAttributes . ReadOnly )
303- {
304- attributes ^= FileAttributes . ReadOnly ;
305- File . SetAttributes ( filePath , attributes ) ;
306- }
307-
308- if ( ! Encoding . GetEncoding ( charset ) . Validate ( File . ReadAllBytes ( filePath ) ) )
309- {
310- Debug . WriteLine ( "Decoding error. " + filePath ) ;
311- continue ;
312- }
313-
314- string content ;
315- using ( StreamReader reader = new StreamReader ( filePath , Encoding . GetEncoding ( charset ) ) )
316- content = reader . ReadToEnd ( ) ;
317-
318- string targetCharset = ( string ) lstConvert . SelectedItem ;
319- Encoding encoding ;
320- // handle UTF-8/16 and UTF-8/16 with BOM
321- switch ( targetCharset )
303+ try
322304 {
323- case "utf-8" :
324- encoding = new UTF8Encoding ( false ) ;
325- break ;
326-
327- case "utf-8-bom" :
328- encoding = new UTF8Encoding ( true ) ;
329- break ;
305+ FileAttributes attributes = File . GetAttributes ( filePath ) ;
306+ if ( ( attributes & FileAttributes . ReadOnly ) == FileAttributes . ReadOnly )
307+ {
308+ attributes ^= FileAttributes . ReadOnly ;
309+ File . SetAttributes ( filePath , attributes ) ;
310+ }
330311
331- case "utf-16" :
332- encoding = new UnicodeEncoding ( bigEndian : false , byteOrderMark : false ) ;
333- break ;
312+ if ( ! Encoding . GetEncoding ( charset ) . Validate ( File . ReadAllBytes ( filePath ) ) )
313+ {
314+ Debug . WriteLine ( "Decoding error. " + filePath ) ;
315+ continue ;
316+ }
334317
335- case "utf-16-bom" :
336- encoding = new UnicodeEncoding ( bigEndian : false , byteOrderMark : true ) ;
337- break ;
318+ string content ;
319+ using ( StreamReader reader = new StreamReader ( filePath , Encoding . GetEncoding ( charset ) ) )
320+ content = reader . ReadToEnd ( ) ;
338321
339- case "utf-16BE" :
340- encoding = new UnicodeEncoding ( bigEndian : true , byteOrderMark : false ) ;
341- break ;
322+ string targetCharset = ( string ) lstConvert . SelectedItem ;
323+ Encoding encoding ;
324+ // handle UTF-8/16 and UTF-8/16 with BOM
325+ switch ( targetCharset )
326+ {
327+ case "utf-8" :
328+ encoding = new UTF8Encoding ( false ) ;
329+ break ;
330+ case "utf-8-bom" :
331+ encoding = new UTF8Encoding ( true ) ;
332+ break ;
333+ case "utf-16" :
334+ encoding = new UnicodeEncoding ( bigEndian : false , byteOrderMark : false ) ;
335+ break ;
336+ case "utf-16-bom" :
337+ encoding = new UnicodeEncoding ( bigEndian : false , byteOrderMark : true ) ;
338+ break ;
339+ case "utf-16BE" :
340+ encoding = new UnicodeEncoding ( bigEndian : true , byteOrderMark : false ) ;
341+ break ;
342+ case "utf-16BE-bom" :
343+ encoding = new UnicodeEncoding ( bigEndian : true , byteOrderMark : true ) ;
344+ break ;
345+ default :
346+ encoding = Encoding . GetEncoding ( targetCharset ) ;
347+ break ;
348+ }
342349
343- case "utf-16BE-bom" :
344- encoding = new UnicodeEncoding ( bigEndian : true , byteOrderMark : true ) ;
345- break ;
350+ using ( StreamWriter writer = new StreamWriter ( filePath , append : false , encoding ) )
351+ {
352+ // TODO: catch exceptions
353+ writer . Write ( content ) ;
354+ writer . Flush ( ) ;
355+ }
346356
347- default :
348- encoding = Encoding . GetEncoding ( targetCharset ) ;
349- break ;
357+ item . Checked = false ;
358+ item . ImageIndex = 0 ;
359+ item . SubItems [ RESULTS_COLUMN_CHARSET ] . Text = targetCharset ;
350360 }
351-
352- using ( StreamWriter writer = new StreamWriter ( filePath , append : false , encoding ) )
361+ catch
353362 {
354- // TODO: catch exceptions
355- writer . Write ( content ) ;
356- writer . Flush ( ) ;
363+ // do nothing
357364 }
358-
359- item . Checked = false ;
360- item . ImageIndex = 0 ;
361- item . SubItems [ RESULTS_COLUMN_CHARSET ] . Text = targetCharset ;
362365 }
363366
364367 // resume drawing of the results list view control
0 commit comments