[英]Renaming a file in C# and excluding the extension of the file
FileInfo currentFile = new FileInfo("c:\\Blue_ 327 132.pdf");
string fileNameFromDB = "c:\\Blue 327 _132.pdf";
string newFileName = fileNameFromDB + currentFile.Extension;
currentFile.MoveTo(newFileName);
我需要用 new filenamefromDB 重命名它
C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\Orange_ 325_ 131.pdf
由於我使用的是 system.io.path 但文件位於 C;\Uploads\..
如果我對多個文件進行循環並且目錄路徑每次都不同怎么辦?
重命名單個文件
FileInfo currentFile = new FileInfo("c:\\Blue_ 327 132.pdf");
currentFile.MoveTo(currentFile.Directory.FullName + "\\" + newName);
其中 newName 是沒有路徑的新名稱。 例如,“new.pdf”
如果您需要保留舊的文件擴展名
FileInfo currentFile = new FileInfo("c:\\Blue_ 327 132.pdf");
currentFile.MoveTo(currentFile.Directory.FullName + "\\" + newName + currentFile.Extension);
重命名多個文件
DirectoryInfo d = new DirectoryInfo("c:\\temp\\");
FileInfo[] infos = d.GetFiles();
foreach(FileInfo f in infos)
{
File.Move(f.FullName, f.FullName.ToString().Replace("abc_","");
}
這是 function System.IO.File.Move的解釋,將文件“移動”到新名稱。
System.IO.File.Move(OldNameFullPath,NewNameFullPath);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.