1010using System . IO . Compression ;
1111using System . Linq ;
1212using System . Reflection ;
13+ using System . Text . RegularExpressions ;
1314using System . Threading . Tasks ;
1415using Windows . ApplicationModel . Core ;
1516using Windows . ApplicationModel . DataTransfer ;
@@ -608,12 +609,54 @@ public void RenameItem_Click(object sender, RoutedEventArgs e)
608609 }
609610 }
610611
612+ public bool ContainsRestrictedCharacters ( string input )
613+ {
614+ Regex regex = new Regex ( "\\ \\ |\\ /|\\ :|\\ *|\\ ?|\\ \" |\\ <|\\ >|\\ |" ) ; //restricted symbols for file names
615+ MatchCollection matches = regex . Matches ( input ) ;
616+ if ( matches . Count > 0 )
617+ {
618+ return true ;
619+ }
620+ return false ;
621+ }
622+
623+ private static readonly List < string > RestrictedFileNames = new List < string > ( )
624+ {
625+ "CON" , "PRN" , "AUX" ,
626+ "NUL" , "COM1" , "COM2" ,
627+ "COM3" , "COM4" , "COM5" ,
628+ "COM6" , "COM7" , "COM8" ,
629+ "COM9" , "LPT1" , "LPT2" ,
630+ "LPT3" , "LPT4" , "LPT5" ,
631+ "LPT6" , "LPT7" , "LPT8" , "LPT9"
632+ } ;
633+
634+ public bool ContainsRestrictedFileName ( string input )
635+ {
636+
637+ foreach ( var name in RestrictedFileNames )
638+ {
639+ Regex regex = new Regex ( $ "^{ name } ($|\\ .)(.+)?") ;
640+ MatchCollection matches = regex . Matches ( input . ToUpper ( ) ) ;
641+ if ( matches . Count > 0 )
642+ {
643+ return true ;
644+ }
645+ }
646+
647+ return false ;
648+ }
649+
611650 public async Task < bool > RenameFileItem ( ListedItem item , string oldName , string newName )
612651 {
613652 if ( oldName == newName )
653+ {
614654 return true ;
655+ }
615656
616- if ( ! string . IsNullOrWhiteSpace ( newName ) )
657+ if ( ! string . IsNullOrWhiteSpace ( newName )
658+ && ! ContainsRestrictedCharacters ( newName )
659+ && ! ContainsRestrictedFileName ( newName ) )
617660 {
618661 try
619662 {
@@ -629,7 +672,6 @@ public async Task<bool> RenameFileItem(ListedItem item, string oldName, string n
629672 }
630673 }
631674 catch ( Exception )
632-
633675 {
634676 var ItemAlreadyExistsDialog = new ContentDialog ( )
635677 {
@@ -673,6 +715,10 @@ public async Task<bool> RenameFileItem(ListedItem item, string oldName, string n
673715 }
674716 }
675717 }
718+ else
719+ {
720+ return false ;
721+ }
676722
677723 CurrentInstance . NavigationToolbar . CanGoForward = false ;
678724 return true ;
0 commit comments