Rename files with C#. Once you have a file in a directory you can simply rename it from your code.

The file will be renamed in the same directory. If you want to rename and move it to a new directory, change the variable to_file.

Rename file in C#

Example

The program below renames an existing file. Make sure the file exists before running the file. You can simply create an empty file.

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

The file will be renamed to a new file.

Rename in shell

Now there are other ways to do this, for example on a Linux or Mac OS X system you can run the command

1
mv source.txt destination.txt

But this may or may not work on other platforms. Thats why you should always use the modules provides by the programming language.