Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How To Rename A File In C#
WhatsApp
Mahesh Chand
5y
275.9
k
0
13
25
Blog
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
Up Next
Copy files from one directory to another directory in C#
Ebook Download
View all
Printing in C# Made Easy
Read by 22.3k people
Download Now!
Learn
View all
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.
Membership not found