Rename a file with C#
This post will discuss how to rename a file using C#.
1. Using File.Move() method
The File.Move() method is used to move a specified file to a new location. The following example demonstrates how to use the File.Move() method for renaming a file. If the file is renamed successfully, a success message is displayed. If the source file doesn’t exist or the destination file is already present, an IOException would be thrown.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.IO; public class Example { public static void Main() { string src = @"C:\some\path\old.file"; string dest = @"C:\some\path\new.file"; try { File.Move(src, dest); if (!File.Exists(src)) { Console.WriteLine("File successfully renamed."); } } catch (IOException e) { Console.WriteLine("The renaming failed: {0}", e.ToString()); } } } |
File.Move throws an IOException when the destination file already exists. We can handle this in two ways:
1. Call the Move overload by setting the overwrite parameter to true, overwriting the destination file if it exists.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.IO; public class Example { public static void Main() { string src = @"C:\some\path\old.file"; string dest = @"C:\some\path\new.file"; File.Move(src, dest, true); if (!File.Exists(src)) { Console.WriteLine("File successfully renamed."); } } } |
2. Call the Delete() method before calling the Move() method, which will delete the destination file if it exists.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.IO; public class Example { public static void Main() { string src = @"C:\some\path\old.file"; string dest = @"C:\some\path\new.file"; File.Delete(dest); File.Move(src, dest); if (!File.Exists(src)) { Console.WriteLine("File successfully renamed."); } } } |
If the source and destination directory are the same, this will delete the source file before moving. This is handled below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; using System.IO; public class Example { public static void Main() { string src = @"C:\some\path\old.file"; string dest = @"C:\some\path\new.file"; if (!src.Equals(dest)) { File.Delete(dest); File.Move(src, dest); if (!File.Exists(src)) { Console.WriteLine("File successfully renamed."); } } } } |
2. Using File.Copy() method
Another approach to renaming a file involves using the File.Copy() method. Here, the idea is to copy the source file to the destination file and then delete the source file. This approach is demonstrated below and should be used only with small files.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System.IO; public class Example { public static void Main() { string src = @"C:\some\path\old.file"; string dest = @"C:\some\path\new.file"; File.Copy(src, dest, true); File.Delete(src); } } |
That’s all about renaming a file with C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)