Know whether the file or directory having permission to delete it in c#

58 views Asked by At

In my application, I'm trying to delete file from code behind but it throws the error called "accessed denied". Anyone can tell me that how to get whether the file is having delete permission or not.

Thanks in advance

1

There are 1 answers

0
keyboardP On

You can just handle the exception

try
{
    Directory.Delete(dirPath);
}
catch (UnauthorizedAccessException ex)
{
    //Access denied
}

If you try to find out before calling Directory.Delete there's a small chance that there's a race condition and something locks the directory which would cause your program to throw an exception. If this exception isn't handled the program will be in an unstable state.