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?
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_","");
}
Here is an explanation of the function System.IO.File.Move , "move" the file to a new name.
System.IO.File.Move(OldNameFullPath,NewNameFullPath);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.