0

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"

panpawel
  • 1,782
  • 1
  • 16
  • 17
  • 4
    Did you check to make sure the destination folder exists? – mason Jul 23 '19 at 18:16
  • 1
    You sure it fails with that **exact** error message, and that nothing else is interfering? I ask because on my system, if `File.Move(x, y)` cannot find the file `x`, it fails with "Could not find file 'x'", not the error message you listed. – Lasse V. Karlsen Jul 23 '19 at 18:17
  • 1
    @mason If the destination folder did not exist it would fail with "Could not find a part of the path." and a `DirectoryNotFoundException`. – Lasse V. Karlsen Jul 23 '19 at 18:17
  • Can you do File.Exists(destinationfile)? – Lost Jul 23 '19 at 18:18
  • @Lost If the destination file already existed you would get `IOException` with "Cannot create a file when that file already exists." – Lasse V. Karlsen Jul 23 '19 at 18:19
  • What is the type of exception you're getting? Can you post more details about the exception object? Such as type, any other properties, such as ErrorCode, HResult, etc. when/if appropriate? – Lasse V. Karlsen Jul 23 '19 at 18:21
  • Destination exists, and the file does not exist in destination. I do check for it but did not include because I want to focus on the problem. – panpawel Jul 23 '19 at 18:27
  • so what is the exception please clarify – misticos Jul 23 '19 at 18:33
  • Here is the exact exception: System.IO.FileNotFoundException: Unable to find the specified file. HResult -2147024894. Source "mscorlib". – panpawel Jul 23 '19 at 18:35
  • This might not be the case, but check if there are duplicated file name in files, while the file is being moved by the first name, the second name check exist and it passes that line, and then it would try to move the file which got just moved. I had an issue with File.Delete and had same file name in the list. – Han Eui-Jun Jul 23 '19 at 18:40
  • @ Han Eui-Jun: File names in files are guarantee to be unique. – panpawel Jul 23 '19 at 18:50
  • This is a debugging problem. Examine all those variables when the exception is thrown. We can't do that for you. – LarsTech Jul 23 '19 at 18:57
  • When I debug, I don't get error. It is a timing issue. – panpawel Jul 23 '19 at 19:11
  • What else moves any files? Are you running this in a thread? File.Exists is a poor choice, because the file might not exist when you check it, but the file might exist when you try to move it. Get rid of the File.Exists and use a Try-Catch instead. – LarsTech Jul 23 '19 at 19:13
  • @LarsTech: The Web Service (implemented elsewhere by someone else) moves the files, called right before I attempt to archive. Yes, I can skip File.Exist, but shouldn't it work and do the job I expect it to do? – panpawel Jul 23 '19 at 19:17
  • Not if you don't have total control of the process, and it sounds like you don't, since the web service is potentially interfering with your files. See [When is it okay to check if a file exists?](https://stackoverflow.com/a/674018/719186) – LarsTech Jul 23 '19 at 19:24
  • @LarsTech, good link, thanks. – panpawel Jul 23 '19 at 19:32

0 Answers0