We can use File.Move or FileInfo.MoveTo methods to rename a file in C#. Here is a code snippet, where the first parameter is the full path of your current file and the second parameter is the name of the new file you want to rename your file to.
- System.IO.File.Move("FullPathOfAfile", "FullPathOfNewFile");
The following code snippet renames a source file into a new file by using the MoveTo method.
- // Source file to be renamed
- string sourceFile = @"C:\Temp\MaheshChand.jpg";
- // Create a FileInfo
- System.IO.FileInfo fi = new System.IO.FileInfo(sourceFile);
- // Check if file is there
- if (fi.Exists)
- {
- // Move file with a new name. Hence renamed.
- fi.MoveTo(@"C:\Temp\Mahesh.jpg");
- Console.WriteLine("File Renamed.");
- }

vinu vijayanPosted Jun 30, 2021, 11:10 AM
This solution is good for small size file. Could you tell the approach need to be use for huge size media files?