My code calls web service that moves some files in the list (a few) from one network share to another network share. Then my code executes foreach, and attempts to move the files in the same list from the same network share to archive folder.
Since some of the files were moved by Web Service, I check File.Exists before each file, and if true, I move it to archive folder. However, many times, File.Exists returns true, but my next instruction, File.Move fails with the error "Unable to find the specified file."
file is a UNC path, eg: \\shareServer\source\a2d6b305-5c97-4d4a-8f92-8eb657235461_401.csv
private void MoveFilesToDestinationFolder(IEnumerable<string> files, string destinationFolder)
{
foreach (var file in files)
{
if (File.Exists(file))
{
string destinationFile = Path.Combine(destinationFolder, Path.GetFileName(file));
try
{
File.Move(file, destinationFile);
}
catch (Exception e)
{
Logger.Write($"{e.Message}. Destination {destinationFile}. File {file}");
}
}
}
}
Is there some kind of caching for File.Exists? Why it returns true, while Move fails?
Edit - per comments asking for the details of the exception:
System.IO.FileNotFoundException: Unable to find the specified file. HResult -2147024894. Source "mscorlib"