0

I am trying to delete a folder but am getting the following error message:

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

string target_dir="D:\\projectpath\\page";
if (Directory.Exists(target_dir))
Directory.Delete(target_dir, false);

How can I resolve this error?

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184

3 Answers3

5

It looks like the file is locked by some other process. This could happen if when reading/writing to it you forgot to dispose the stream reader/writer and you leaked the unmanaged handler to the file.

For example if you used the following code to read from the file:

StreamReader reader = new StreamReader(fileName);
string contents = reader.ReadToEnd();

and you never release the reader, the file will be locked. The proper way is to wrap IDisposable resources such as Streams and StreamReaders in using statements:

using (StreamReader reader = new StreamReader(fileName))
{
    string contents = reader.ReadToEnd();
}

If on the other hand the file is locked by some other external process to your application then there's very little you could do about it, other than killing this process.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

I think on the surface, your problem should be apparent: the file is in use by something else, so you can't delete the directory it resides in. If there was a way to "force delete" the file, it could cause other programs to crash. I'd recommend catching the error and either logging it or displaying it to the user, so they can decide if they really want to delete the in-use file.

If you MUST delete the file, you could take a look at:

Using C#, how does one figure out what process locked a file?

And once you know what the process is, you can then kill it, which should free up the file. Again, this isn't a good practice and should only be used in exceptional circumstances.

Community
  • 1
  • 1
User
  • 1,118
  • 7
  • 23
0

To delete the diectory you must have the correct Permissions.

 var target_dir = "D:\\projectpath\page";
  var isWriteAccess = false;
  try
  {
    var collection = Directory.GetAccessControl(target_dir)
      .GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
    if (collection.Cast<FileSystemAccessRule>().Any(rule => rule.AccessControlType == AccessControlType.Allow))
    {
      isWriteAccess = true;
    }
  }
  catch (UnauthorizedAccessException ex)
  {
    isWriteAccess = false;
  }
  catch (Exception ex)
  {
    isWriteAccess = false;
  }

  if (!isWriteAccess)
  {
    MessageBox.Show("no access to directory.");
    // Handle here close and kill the blocking process

  }
  else
  {
      Directory.Delete(target_dir, false);
  }
}
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
  • Honestly, I don't think it's a permission issue, as the error displays the next message: "The process cannot access the file .it is being used by another process". – sockfd2 Aug 23 '13 at 15:02
  • Yeah but that means you do not have a write permission to the diectory for any reason! You have first to detect if you can delete this file and if you can than just delete else look for the reason if another process block that than kill it if a streamer is still using the file than close it etc – Bassam Alugili Aug 23 '13 at 15:06