Programming
c# file rename
Updated Sat, 06 Aug 2022 03:04:45 GMT

Rename a file in C#


How do I rename a file using C#?




Solution

Take a look at System.IO.File.Move, "move" the file to a new name.

System.IO.File.Move("oldfilename", "newfilename");




Comments (5)

  • +0 – This solution does not work when file names differ only in letter case. For example file.txt and File.txt — Jul 06, 2014 at 20:31  
  • +3 – @SepehrM, I just double checked and it works fine on my Windows 8.1 machine. — Jul 07, 2014 at 01:57  
  • +1 – @SepehrM, I did not test it, but the samples you point to use FileInfo.Move and not File.Move so maybe that has something to do with it? — Jul 07, 2014 at 12:31  
  • +3 – @SepehrM Windows file system names are case insensitive. File.txt and file.txt are treated as the same file name. So it's not clear to me when you say the solution doesn't work. What are you doing exactly that isn't working? — Oct 17, 2016 at 17:35  
  • +5 – @Michael, the file system is case insensitive, but it does store the filename in the original case as entered by the user. In SepehrM's case, he was trying to change the case of a file, which for some reason was not working. The case insensitive matching was working. HTH — Oct 18, 2016 at 00:08  


External Links

External links referenced by this document: