如下C#實作對檔案的重命名的方法需要傳入三個string型別的引數,分別是源檔案的檔案目錄、目的檔案目錄和重命名的檔案名稱,實作代碼如下:
public ExecutionResult FileRename(string sourceFile, string destinationPath, string destinationFileName) { ExecutionResult result; FileInfo tempFileInfo; FileInfo tempBakFileInfo; DirectoryInfo tempDirectoryInfo; result = new ExecutionResult(); tempFileInfo = new FileInfo(sourceFile); tempDirectoryInfo = new DirectoryInfo(destinationPath); tempBakFileInfo = new FileInfo(destinationPath + "\\" + destinationFileName); try { if (!tempDirectoryInfo.Exists) tempDirectoryInfo.Create(); if (tempBakFileInfo.Exists) tempBakFileInfo.Delete(); //move file to bak tempFileInfo.MoveTo(destinationPath + "\\" + destinationFileName); result.Status = true; result.Message = "Rename file OK"; result.Anything = "OK"; } catch (Exception ex) { result.Status = false; result.Anything = "Mail"; result.Message = ex.Message; if (mesLog.IsErrorEnabled) { mesLog.Error(MethodBase.GetCurrentMethod().Name, "Rename file error. Msg :" + ex.Message); mesLog.Error(ex.StackTrace); } } return result; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/82981.html
標籤:C#
上一篇:C#呼叫7z實作檔案的壓縮與解壓
