TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How To Rename A File In C#
Mahesh Chand
Aug 26, 2019
267.5
k
0
13
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
We can use FileInfo.MoveTo method to rename a file in C#.
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"
);
Here is the complete code of using FileInfo class. It does same as the above.
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."
);
}
Detailed tutorials:
Working With FileInfo Class In C#
Working With File Class In C#
Working With Directories and Folders In C#
c# file rename
FileInfo.MoveTo
Next Recommended Reading
Copy files from one directory to another directory in C#
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.