if (!FileInUse(FullFileName, ref sMessage)) { File.Move(FullFileName, fullArchiveFileLocation); bArchiveSucceeded = true; } else { EventLogBusinessLogic.CreateEventLog("Move Failed", "Move failed for file " + FullFileName + " Message: " + sMessage, DateTime.Now, eEventType.None); Thread.Sleep(1000); //sleep for a second before trying again } static bool FileInUse(string path, ref string Message) { try { //Just opening the file as open/create using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate)) { } return false; } catch (IOException ex) { //check if message is for a File IO Message = ex.Message.ToString(); return true; } }
https://msdn.microsoft.com/en-us/library/system.io.file.move%28v=vs.110%29.aspx[^]If you try to move a file across disk volumes and that file is in use, the file is copied to the destination, but it is not deleted from the source.
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)