Click here to Skip to main content
15,884,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, I have an issue that maybe someone can help with.

Basically I have an application that transfers 100's of files from one local server to another, every now and again (not all the time) I see an issue where System.IO.File.Move only copies the file to the destination location and leaves the original. The code below is the code I use for moving the file, The file definitely does not exist in the destination location before the move.

C#
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;
           }
       }


Any ideas as to why this is happening or has anyone seen this before, any help is very much appreciated.
Posted
Updated 11-Apr-15 3:58am
v2
Comments
ZurdoDev 10-Apr-15 10:11am    
From 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."

This might be the case (quote from MSDN):
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Apr-15 11:10am    
5ed.
—SA
Thomas Daniels 10-Apr-15 11:12am    
Thank you!
frostcox 11-Apr-15 10:00am    
Hi Thanks for you reply, I looked in to this and I made a few adjustments to the code to try and get to the bottom of this. Unfortunately there are some files still only being copied as apposed to being moved. Just wondering have you any other ideas... im stumped!!
Thomas Daniels 11-Apr-15 10:03am    
Is the file you are trying to move in use?
frostcox 11-Apr-15 10:28am    
By the looks of it no, as if it was it should return true, therefor entering a record in my event log table. see code above.
Things are not always what they seem when it comes to the windows file system and some time i have noticed that even when a folder is empty according to windows that you still cannot delete the folder.

You can make an API call to windows to ask for a list of all the files that are locked and windows will reply with an incomplete list.

Under the cover windows is copying files all over the place and file manager when using webdav to copy files first downloads the file and then starts running the green progress bar as it copies the file from a temp location to the new location.

try using the odd Thread.sleep() in your code to lets windows catch up if you are running a tight loop
 
Share this answer
 
Comments
frostcox 13-Apr-15 3:56am    
Hey, Thanks very much i will give it a try.
Just to add some files use a digital certificate like C:\Windows\System32\TsWpfWrp.exe and you will get all sorts of trouble playing with them.

I just tried to take a copy of System32 and windows file manager came up with lots of errors because the files are locked but its not a very good system when the only way to copy this folder is to boot up using something like Tails to take a copy
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900