I've an application which after generate a file, tries to move it from a network location to a local folder using the File.Move function .
File.Move(_backupSource + @"\" + filename, _backupDestination + @"\" + filename);
_backupSource is the network location and _backupDestintation is the local folder.
The file is copied to the destination location, from a few days is not deleting the file on the Source, like if I used File.Copy, but this aplication was working fine for two months now.
I also looked at the event viewer from the two servers and no exception was generated.
¿What could happen?
EDIT: One thing that could be important. This app is triggered by the task scheduler every night. If I run the aplication manually, the file is moved (not copied)
EDIT 2: Now I've tried to copy the file and then delete the file using the following method
private static void MoveFiles(string filename)
{
FileInfo file = new FileInfo(_backupSource + @"\" + filename);
FileStream stream = null;
int iteration = 0;
file.CopyTo(_backupDestination + @"\" + filename);
bool isUnlocked = false;
while (!isUnlocked)
{
iteration++;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
isUnlocked = true;
}
catch (IOException ex)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
Thread.Sleep(60 * 1000);
if (iteration > 60)
throw ex;
else
EventLog.WriteEntry("DailyRestore", string.Format("Move File. Iteration number {0}", filename), EventLogEntryType.Information);
}
finally
{
if (stream != null)
stream.Close();
}
}
file.Delete();
}
In the log can see the app was trying by an hour each minute and the file is always "using by another process". Like I mention, this is when the exe is executed by the scheduler task, if I run the task that execute the exe manually (right click and execute), all the operations are successfully ended.