Shambavi
Shambavi

Reputation: 319

Directory Rename Exception in C#

When I try to rename a Directory using the following code:

  try
  {
        System.IO.Directory.Move(oldPath, newPath);
  }
  catch (System.IO.IOException e2)
  {
        Console.WriteLine(e2.Message);
  }

I get the following exception: The process cannot access the file because it is being used by another process.

  1. I don't understand why it says "file" in the first place.
  2. Also, the directory is empty. What file is it referring to?
  3. Lastly, how to manage to rename the Directory without any exceptions?

UPDATE: I guess I found the reason for the exception, it is because I am trying to rename the file/folder names of files/folders situated in the Google Drive. The Google Drive application is the other process using it! Any solutions to rename a folder in the Google Drive? But the weird thing is that I don't get this exception when I try to rename files located in the Google Drive through C#.

Thanks!

Upvotes: 0

Views: 1324

Answers (3)

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

1 Verify that newPath already does not exist

http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx

2 Verify that your directory does not contain opened file

Upvotes: 0

rein
rein

Reputation: 33445

  1. It says "file" because the underlying commands are probably using a generic move command for both directories and files and the localized error string contains the word file.
  2. Even if a directory is empty, it can be locked for any edits if you have it "open" in an application (do you have explorer Windows open in the directory, is the command prompt current directory set to the one you want to move/delete)?
  3. Unfortunately there's not much you can do unless you want to start killing offending processes programmatically.

You can use Process Explorer to find the process that has a lock on that folder. See http://windowsxp.mvps.org/processlock.htm for more info.

Upvotes: 0

Ilya Ivanov
Ilya Ivanov

Reputation: 23626

Your folder seems to be in use by another process. Try to close your explorer or other programs, that use that folder. If nothing help - try to restart your machine. If those won't help - consider using Unlocker to free folder from usage of another process. Note that it would be weird, if non-system folder is occupied after restart of the machine

Upvotes: 1

Related Questions