-1

I am using File.GetCreationTime to sort few files. After using this function when I am trying to move using File.Move I get an error:

The process cannot access the file because it is being used by another process.

Here is the code:

            foreach (var f in Directory.GetFiles(source))
            {
                DateTime creation = File.GetCreationTime(f);
                var fileDest = Path.Combine(destination, creation.Year.ToString());
                if (!Directory.Exists(fileDest))
                {
                    Directory.CreateDirectory(fileDest);
                }

                File.Move(f, Path.Combine(fileDest, Path.GetFileName(f)));
            }

Does File.GetCreationTime gets the filehandle? What am I doing wrong?

I have used the retry pattern suggested but still the code fails:

            foreach (var f in Directory.GetFiles(source))
            {
                DateTime creation = File.GetCreationTime(f);
                var fileDest = Path.Combine(destination, creation.Year.ToString());
                if (!Directory.Exists(fileDest))
                {
                    Directory.CreateDirectory(fileDest);
                }

                for (int i = 1; i <= 3; ++i)
                {
                    try
                    {
                        File.Move(f, Path.Combine(fileDest, Path.GetFileName(f)));
                        break;
                    }
                    catch (IOException e)
                    {
                        if (i == 3)
                            throw;

                        Thread.Sleep(1000);
                    }
                }
            }
Suresh
  • 25
  • 5
  • Are you sure it's `File.GetCreationTime(f);` that is throwing the exception? It seems more likely that `File.Move()` is throwing. – Matthew Watson Jul 07 '16 at 11:44
  • @MatthewWatson that's exactly what the OP said "when I am trying to move " – Zein Makki Jul 07 '16 at 11:51
  • Possible duplicate of [IOException: The process cannot access the file 'file path' because it is being used by another process](http://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being) – meJustAndrew Jul 07 '16 at 12:08
  • I don't know why -1, how any body can say I didn't research? I didn't find answer else where that's the reason to post it here. If you can point me to right resources it would be helpful not by giving -1. – Suresh Jul 07 '16 at 12:10
  • @meJustAndrew in that post the `File.Open` or `File.Read` operations were done which opens the file stream. But here it is just `File.GetCreationTime`. I don't think they both are same – Suresh Jul 07 '16 at 12:17
  • 1
    @user3185569 The title of this question says: *"File.GetCreationTime throws The process cannot access the file because it is being used by another process"*. It doesn't say *"File.Move() throws..."* Hence the confusion. – Matthew Watson Jul 07 '16 at 12:21
  • 1
    This is failing because you can't move a file which is currently open - and something ELSE other than your code has it open. – Matthew Watson Jul 07 '16 at 12:22
  • @MatthewWatson sorry the question was wrong I should have stated after `File.GetCreationTime` any IO operstion throws error... Hope I can edit the question. But I am sure no other process has opened it. You can give a try – Suresh Jul 07 '16 at 12:29

1 Answers1

0

The error message already tells you exactly what the problem is: You cannot move the file because someone is using it.

A simple way to find out which process has locked the file is to use Process Explorer. Just select Find > Find Handle or DLL... from the menu and enter the name of your file et voilà, the name of the process is in the results:

enter image description here

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • It says the current exe name. But what else in the code could open the file? – Suresh Jul 07 '16 at 12:36
  • @Suresh: Go through your code and see where the file is accessed. Probably you are missing a call to `Close()` somewhere or did not wrap the usage of the file with a `using` block that would automatically release the file. – Dirk Vollmar Jul 07 '16 at 12:40
  • @DrikVollmar That is just the only peace of code I wrote in a console application file. – Suresh Jul 07 '16 at 12:43