FileUtils.cs 758 B

123456789101112131415161718192021222324252627282930
  1. using System.IO;
  2. namespace GodotTools.Core
  3. {
  4. public static class FileUtils
  5. {
  6. public static void SaveBackupCopy(string filePath)
  7. {
  8. string backupPathBase = filePath + ".old";
  9. string backupPath = backupPathBase;
  10. const int maxAttempts = 5;
  11. int attempt = 1;
  12. while (File.Exists(backupPath) && attempt <= maxAttempts)
  13. {
  14. backupPath = backupPathBase + "." + (attempt);
  15. attempt++;
  16. }
  17. if (attempt > maxAttempts + 1)
  18. {
  19. // Overwrite the oldest one
  20. backupPath = backupPathBase;
  21. }
  22. File.Copy(filePath, backupPath, overwrite: true);
  23. }
  24. }
  25. }