FileUtils.cs 665 B

123456789101112131415161718192021222324252627
  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. return;
  19. File.Copy(filePath, backupPath, overwrite: true);
  20. }
  21. }
  22. }