c# - File.Move Not Working, No Error -
i'm running issues trying move flat html files around on server.
in short, process says should take whatever file in newpath (if there one) , move backup folder. take file in old path , move new path. checks see if successful (i.e. newpath exist) , if wasn't, replaces backup. i've pasted method below viewing pleasure.
public bool movecontent(string oldpath, string newpath, string backupdirectorypath, out string backuppath) { backuppath = string.empty; var oldfilepath = httpcontext.current.server.mappath(oldpath); var newfilepath = httpcontext.current.server.mappath(newpath); var newdirectory = path.getdirectoryname(newfilepath); // if file we're moving doesn't exist, fail. if (!file.exists(oldfilepath)) throw new invalidpathexception(oldfilepath); // if no destination found, fail. if (string.isnullorwhitespace(newdirectory)) throw new invalidpathexception(newfilepath); if (!directory.exists(newdirectory)) directory.createdirectory(newdirectory); var backupphysicalpath = string.empty; // if there file in our destination, 1 up. if (file.exists(newfilepath) && !string.isnullorwhitespace(backupdirectorypath)) { var backupfilepath = httpcontext.current.server.mappath(backupdirectorypath); var backupdirectory = path.getdirectoryname(backupfilepath); // if backup destination doesn't exist, fail. if(string.isnullorwhitespace(backupdirectory)) throw new invalidpathexception(backupdirectory); if(!directory.exists(backupdirectory)) directory.createdirectory(backupdirectory); var filename = path.getfilenamewithoutextension(newfilepath); var currentdatetime = datetime.now.tostring(filehelpers.tempfiledateformat); var fileextension = path.getextension(newfilepath); // example result: hardware-2015-01-30-08-35-26-475.html backuppath = backupdirectorypath.replace(filename, filename + "-" + currentdatetime); backupphysicalpath = string.format("{0}\\{1}-{2}{3}", backupdirectory, filename, currentdatetime, fileextension); // if there file in our backup destination, fail. if (file.exists(backupphysicalpath)) throw new invalidpathexception(backupphysicalpath); // backup file exists in our new destination. file.move(newfilepath, backupphysicalpath); } // move our file new destination. file.move(oldfilepath, newfilepath); // return false if new file doesn't exist. if (!file.exists(newfilepath)) { // if made backup, return backup original loction, since there's nothing in destination. if (!string.isnullorwhitespace(backupphysicalpath) && file.exists(backupphysicalpath)) { file.move(backupphysicalpath, newfilepath); } throw new exception(string.format("failed move content. oldpath: '{0}'; newpath: '{1}'; backuppath: '{2}'", oldfilepath, newfilepath, backupphysicalpath)); } return true; }
here's example of parameters being passed in:
oldpath: "/client/content/en/unpublished/anpan.html" newpath: "/client/content/en/anpan.html" backupdirectorypath: "/client/content/en/backups/anpan.html"
the problem i'm running backup file made (it move newpath backuppath), won't move oldpath newpath.
i've been unable reproduce issue happens infrequently , without exceptions being thrown, symptoms exist (i can see files on filesystem when client reports issue) , it's been reported multiple times.
i put logging around , wrapped entire method in try/catch. never fails unexpectedly (except when throw invalidpathexception). there nothing in logs when happens.
can me diagnose issue, or tell me if i'm doing wrong in method cause problem?
thanks much!
Comments
Post a Comment