@@ -25,14 +25,14 @@ public static class ProfileManager
2525 private const string ProfilesFolderName = "Profiles" ;
2626
2727 /// <summary>
28- /// Default profile name.
28+ /// Profiles backups directory name.
2929 /// </summary>
30- private const string ProfilesDefaultFileName = "Default ";
30+ private static string BackupFolderName => "Backups ";
3131
3232 /// <summary>
33- /// Profiles backups directory name.
33+ /// Default profile name.
3434 /// </summary>
35- private static string BackupFolderName => "Backups ";
35+ private const string ProfilesDefaultFileName = "Default ";
3636
3737 /// <summary>
3838 /// Profile file extension.
@@ -290,14 +290,21 @@ public static void CreateEmptyProfileFile(string profileName)
290290 /// <param name="newProfileName">New <see cref="ProfileFileInfo.Name" /> of the profile file.</param>
291291 public static void RenameProfileFile ( ProfileFileInfo profileFileInfo , string newProfileName )
292292 {
293+ // Check if the profile is currently in use
293294 var switchProfile = false ;
294-
295+
295296 if ( LoadedProfileFile != null && LoadedProfileFile . Equals ( profileFileInfo ) )
296297 {
297298 Save ( ) ;
298299 switchProfile = true ;
299300 }
300301
302+ // Create backup
303+ Backup ( profileFileInfo . Path ,
304+ GetProfilesBackupFolderLocation ( ) ,
305+ TimestampHelper . GetTimestampFilename ( Path . GetFileName ( profileFileInfo . Path ) ) ) ;
306+
307+ // Create new profile info with the new name
301308 ProfileFileInfo newProfileFileInfo = new ( newProfileName ,
302309 Path . Combine ( GetProfilesFolderLocation ( ) , $ "{ newProfileName } { Path . GetExtension ( profileFileInfo . Path ) } ") ,
303310 profileFileInfo . IsEncrypted )
@@ -306,15 +313,18 @@ public static void RenameProfileFile(ProfileFileInfo profileFileInfo, string new
306313 IsPasswordValid = profileFileInfo . IsPasswordValid
307314 } ;
308315
316+ // Copy the profile file to the new location
309317 File . Copy ( profileFileInfo . Path , newProfileFileInfo . Path ) ;
310318 ProfileFiles . Add ( newProfileFileInfo ) ;
311319
320+ // Switch profile, if it was previously loaded
312321 if ( switchProfile )
313322 {
314323 Switch ( newProfileFileInfo , false ) ;
315324 LoadedProfileFileChanged ( LoadedProfileFile , true ) ;
316325 }
317326
327+ // Remove the old profile file
318328 File . Delete ( profileFileInfo . Path ) ;
319329 ProfileFiles . Remove ( profileFileInfo ) ;
320330 }
@@ -353,6 +363,11 @@ public static void EnableEncryption(ProfileFileInfo profileFileInfo, SecureStrin
353363 switchProfile = true ;
354364 }
355365
366+ // Create backup
367+ Backup ( profileFileInfo . Path ,
368+ GetProfilesBackupFolderLocation ( ) ,
369+ TimestampHelper . GetTimestampFilename ( Path . GetFileName ( profileFileInfo . Path ) ) ) ;
370+
356371 // Create a new profile info with the encryption infos
357372 var newProfileFileInfo = new ProfileFileInfo ( profileFileInfo . Name ,
358373 Path . ChangeExtension ( profileFileInfo . Path , ProfileFileExtensionEncrypted ) , true )
@@ -361,9 +376,9 @@ public static void EnableEncryption(ProfileFileInfo profileFileInfo, SecureStrin
361376 IsPasswordValid = true
362377 } ;
363378
364- List < GroupInfo > profiles = Path . GetExtension ( profileFileInfo . Path ) == LegacyProfileFileExtension
365- ? DeserializeFromXmlFile ( profileFileInfo . Path )
366- : DeserializeFromFile ( profileFileInfo . Path ) ;
379+ List < GroupInfo > profiles = Path . GetExtension ( profileFileInfo . Path ) == LegacyProfileFileExtension ?
380+ DeserializeFromXmlFile ( profileFileInfo . Path ) :
381+ DeserializeFromFile ( profileFileInfo . Path ) ;
367382
368383 // Save the encrypted file
369384 var decryptedBytes = SerializeToByteArray ( profiles ) ;
@@ -409,7 +424,12 @@ public static void ChangeMasterPassword(ProfileFileInfo profileFileInfo, SecureS
409424 switchProfile = true ;
410425 }
411426
412- // Create a new profile info with the encryption infos
427+ // Create backup
428+ Backup ( profileFileInfo . Path ,
429+ GetProfilesBackupFolderLocation ( ) ,
430+ TimestampHelper . GetTimestampFilename ( Path . GetFileName ( profileFileInfo . Path ) ) ) ;
431+
432+ // Create new profile info with the encryption infos
413433 var newProfileFileInfo = new ProfileFileInfo ( profileFileInfo . Name ,
414434 Path . ChangeExtension ( profileFileInfo . Path , ProfileFileExtensionEncrypted ) , true )
415435 {
@@ -423,11 +443,9 @@ public static void ChangeMasterPassword(ProfileFileInfo profileFileInfo, SecureS
423443 GlobalStaticConfiguration . Profile_EncryptionKeySize ,
424444 GlobalStaticConfiguration . Profile_EncryptionIterations ) ;
425445
426- List < GroupInfo > profiles ;
427-
428- profiles = IsXmlContent ( decryptedBytes )
429- ? DeserializeFromXmlByteArray ( decryptedBytes )
430- : DeserializeFromByteArray ( decryptedBytes ) ;
446+ List < GroupInfo > profiles = IsXmlContent ( decryptedBytes ) ?
447+ DeserializeFromXmlByteArray ( decryptedBytes ) :
448+ DeserializeFromByteArray ( decryptedBytes ) ;
431449
432450 // Save the encrypted file
433451 decryptedBytes = SerializeToByteArray ( profiles ) ;
@@ -468,7 +486,12 @@ public static void DisableEncryption(ProfileFileInfo profileFileInfo, SecureStri
468486 switchProfile = true ;
469487 }
470488
471- // Create a new profile info
489+ // Create backup
490+ Backup ( profileFileInfo . Path ,
491+ GetProfilesBackupFolderLocation ( ) ,
492+ TimestampHelper . GetTimestampFilename ( Path . GetFileName ( profileFileInfo . Path ) ) ) ;
493+
494+ // Create new profile info
472495 var newProfileFileInfo = new ProfileFileInfo ( profileFileInfo . Name ,
473496 Path . ChangeExtension ( profileFileInfo . Path , ProfileFileExtension ) ) ;
474497
@@ -478,9 +501,10 @@ public static void DisableEncryption(ProfileFileInfo profileFileInfo, SecureStri
478501 GlobalStaticConfiguration . Profile_EncryptionKeySize ,
479502 GlobalStaticConfiguration . Profile_EncryptionIterations ) ;
480503
481- List < GroupInfo > profiles = IsXmlContent ( decryptedBytes )
482- ? DeserializeFromXmlByteArray ( decryptedBytes )
483- : DeserializeFromByteArray ( decryptedBytes ) ;
504+ List < GroupInfo > profiles = IsXmlContent ( decryptedBytes ) ?
505+ DeserializeFromXmlByteArray ( decryptedBytes ) :
506+ DeserializeFromByteArray ( decryptedBytes ) ;
507+
484508 // Save the decrypted profiles to the profile file
485509 SerializeToFile ( newProfileFileInfo . Path , profiles ) ;
486510
0 commit comments