1010using Syncfusion . Windows . Controls . RichTextBoxAdv ;
1111using System ;
1212using System . IO ;
13+ using System . Windows ;
1314using System . Windows . Controls ;
15+ using System . Windows . Input ;
1416using System . Windows . Media ;
1517using System . Windows . Threading ;
1618
@@ -20,9 +22,42 @@ internal static class SyncfusionControl
2022{
2123 public static Control Open ( string path )
2224 {
23- if ( new FileInfo ( path ) . IsReadOnly )
25+ // The Syncfusion method we are currently using does not support reading read-only file
26+ if ( ( File . GetAttributes ( path ) & FileAttributes . ReadOnly ) == FileAttributes . ReadOnly )
2427 {
25- return new Label { Content = "Readonly file is not supported." } ;
28+ return new ContentControl ( )
29+ {
30+ Content = new StackPanel ( )
31+ {
32+ Children =
33+ {
34+ new Label { Content = "Read-only file is not supported." } ,
35+ new Button
36+ {
37+ Content = "Remove read-only file attribute" ,
38+ Margin = new Thickness ( 0 , 10 , 0 , 0 ) ,
39+ Command = new RelayCommand ( ( ) =>
40+ {
41+ try
42+ {
43+ FileAttributes attributes = File . GetAttributes ( path ) ;
44+
45+ if ( ( attributes & FileAttributes . ReadOnly ) == FileAttributes . ReadOnly )
46+ {
47+ // Try to remove read-only file attribute
48+ File . SetAttributes ( path , attributes & ~ FileAttributes . ReadOnly ) ;
49+ MessageBox . Show ( "Removed readonly file attribute successfully, please try to open again." , "Success" , MessageBoxButton . OK , MessageBoxImage . Information ) ;
50+ }
51+ }
52+ catch ( Exception ex )
53+ {
54+ MessageBox . Show ( ex . Message , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
55+ }
56+ } ) ,
57+ }
58+ }
59+ }
60+ } ;
2661 }
2762
2863 return ( Path . GetExtension ( path ) ? . ToLower ( ) ) switch
@@ -108,3 +143,19 @@ private static Control OpenPowerpoint(string path)
108143 return viewer ;
109144 }
110145}
146+
147+ file sealed class RelayCommand ( Action execute , Func < bool > canExecute = null ) : ICommand
148+ {
149+ private readonly Action _execute = execute ?? throw new ArgumentNullException ( nameof ( execute ) ) ;
150+ private readonly Func < bool > _canExecute = canExecute ;
151+
152+ public bool CanExecute ( object parameter ) => _canExecute == null || _canExecute ( ) ;
153+
154+ public void Execute ( object parameter ) => _execute ( ) ;
155+
156+ public event EventHandler CanExecuteChanged
157+ {
158+ add => CommandManager . RequerySuggested += value ;
159+ remove => CommandManager . RequerySuggested -= value ;
160+ }
161+ }
0 commit comments