1

I have what I thought would be a very simple file mover script. It checks for a file and moves it to a new directory if it exists:

if (File.Exists(_collection[x,0]))
{
    System.IO.File.Move(_collection[x, 0], _moveTo);
    MessageBox.Show("File moved because it was stale.");
}

It passes the check that the file exists, but then errors on the following line when trying to move it stating that the file is being used by another process. I can only assume that File.Exists is causing it to hang up somehow, but can't find a solution from anyone else who had this problem.

Dazzler
  • 380
  • 3
  • 27
Marshal Alessi
  • 105
  • 2
  • 2
  • 11
  • Is the `_collection` an array of `FileInfo` or of `String`? – Icepickle Jul 09 '17 at 09:53
  • Did you open the file at any point? Could the program not have closed properly? – pookie Jul 09 '17 at 09:55
  • _collection holds a string, referring to the actual path of the file. And @pookie no the file has not been opened, and the program has been restarted so I don't think there should be any remaining links open to it. – Marshal Alessi Jul 09 '17 at 10:04
  • can you use this file in other place? workaround solution is copy your file into new folder then delete old file. – C.Fasolin Jul 09 '17 at 10:05
  • @C.Fasolin are you suggesting I create a temp directory to store it and move it to it's final destination from there? If so, I could try. But wouldn't it still have an error trying to move to the temp directory? – Marshal Alessi Jul 09 '17 at 10:07
  • So..It seems having the directory(not the file) open caused the initial error. But even with that closed(why can't I have the directory open?!!?!) I'm getting a "Cannot create a file when that file already exists." exception. – Marshal Alessi Jul 09 '17 at 10:13
  • So what is in `_moveTo` variable? Note it should be a full path, **including** the name of the new file. If it's a folder, you need something like `File.Move(_collection[x, 0], Path.Combine(_moveTo, Path.GetFileName(_collection[x, 0])));` – Ivan Stoev Jul 09 '17 at 10:19
  • _"I can only assume that File.Exists is causing it to hang up somehow, but can't find a solution from anyone else who had this problem."_ - that's because your assumption is incorrect. _You_ are locking the file elsewhere. – CodeCaster Jul 09 '17 at 10:31

2 Answers2

3

try this code:

    string filePathNameToMove = "";
    string directoryPathToMove = "";

    if (File.Exists(filePathNameToMove))
    {
        string destinationFilePathName = 
               Path.Combine(directoryPathToMove, Path.GetFileName(filePathNameToMove));
        if (!File.Exists(destinationFilePathName))
        {
            try
            {
                File.Move(filePathNameToMove, destinationFilePathName);
                Console.WriteLine("File Moved!");
            }
            catch (Exception e)
            {
                Console.WriteLine("File Not Moved! Error:" + e.Message);

            }
        }
    }
TaW
  • 53,122
  • 8
  • 69
  • 111
C.Fasolin
  • 313
  • 1
  • 4
  • 19
  • 4
    Because "try this" is not an answer. An answer should explain what's wrong with the OP's code, then explain how to solve that problem and ultimately showing the code to do that. You just dumped some code wrapped in a try-catch block, which hides the error but doesn't solve the root cause. – CodeCaster Jul 09 '17 at 10:30
  • 1
    I downvoted because you a) didn't explain and b) wrote if (bool == false) instead of if(!bool). But the solution itself is correct: The move destination must be a filename, not just a path. – TaW Jul 09 '17 at 10:33
  • 2
    OK thk for explication, for bool==false I like this signature because is more readingable to !bool – C.Fasolin Jul 09 '17 at 10:37
  • Thank you for your responses. It is now my understanding that you have to combine the filepath prior to moving it, which appeared to be the end solution. Thank you. – Marshal Alessi Jul 09 '17 at 10:37
3

In case anyone else has this problem. In my case, the file had been opened in Excel and Excel was never garbage collected after being terminated. So the OS still thought the file was being accessed. I did the following, crude, but it works.

                for (int i = 1; i > 0; i++)
                {
                     try
                     {
                         File.Move(sourceFileName, destinationFileName);
                         break;
                     } catch
                     {
                         GC.Collect();
                     }
                }
JasonG
  • 127
  • 1
  • 8