Reputation: 95
FileInfo currentFile = new FileInfo("c:\\Blue_ 327 132.pdf");
string fileNameFromDB = "c:\\Blue 327 _132.pdf";
string newFileName = fileNameFromDB + currentFile.Extension;
currentFile.MoveTo(newFileName);
I need rename it with new filenamefromDB
C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\Orange_ 325_ 131.pdf
Since I'm using system.io.path but the files are present under C;\Uploads\..
What if I have do looping for more than one files and directory path varies every time?
Upvotes: 4
Views: 16754
Reputation: 21
Here is an explanation of the function System.IO.File.Move, "move" the file to a new name.
System.IO.File.Move(OldNameFullPath,NewNameFullPath);
Upvotes: 0
Reputation: 6814
To rename a single file
FileInfo currentFile = new FileInfo("c:\\Blue_ 327 132.pdf");
currentFile.MoveTo(currentFile.Directory.FullName + "\\" + newName);
where newName is your new name without path. For example, "new.pdf"
If you need to keep old file extension
FileInfo currentFile = new FileInfo("c:\\Blue_ 327 132.pdf");
currentFile.MoveTo(currentFile.Directory.FullName + "\\" + newName + currentFile.Extension);
To rename multiple files
DirectoryInfo d = new DirectoryInfo("c:\\temp\\");
FileInfo[] infos = d.GetFiles();
foreach(FileInfo f in infos)
{
File.Move(f.FullName, f.FullName.ToString().Replace("abc_","");
}
Upvotes: 10