0

So I'm using File.Copy to create a Backup of a text file everytime the program runs, the files exist, and the paths are fine but File.Copy seemingly won't do anything with said file.

Minimalised, but the code below is what I'm using.

string FilePath = "F:\\Royal Court Bot\\FileRAM.txt";
File.Copy(FilePath, FilePath[..4] + "Backup.txt", true);

When the code runs, no exception is thrown (Checked even with a try-catch) but nothing else seems to happen either.

BigBoronto
  • 33
  • 6
  • Does this answer your question? [how to take all array elements except last element in C#](https://stackoverflow.com/questions/3264845/how-to-take-all-array-elements-except-last-element-in-c-sharp) – Charlieface Apr 30 '23 at 10:36
  • `FilePath[..^4]` is what you need – Charlieface Apr 30 '23 at 10:36

1 Answers1

1

The result of FilePath[..4] + "Backup.txt" is F:\RBackup.txt.

Is it really the destination do you want to store the backup file ?

If you want is the same location with original file, try :

string FilePath = "F:\\Royal Court Bot\\FileRAM.txt";
FileInfo FileInfo = new FileInfo(FilePath);   
File.Copy(FilePath, Path.Combine(FileInfo.DirectoryName, "Backup.txt"), true);
VietDD
  • 1,048
  • 2
  • 12
  • 12