How do I check whether File.Delete() will succeed without trying it, in C#?

21,892

Solution 1

The problem with implementing FileIsDeletableByCurrentUser is that it's not possible to do so. The reason is the file system is a constantly changing item.

In between any check you make to the file system and the next operation any number of events can and will happen. Including ...

  • Permissions on the file could change
  • The file could be deleted
  • The file could be locked by another user / process
  • The USB key the file is on could be removed

The best function you could write would most aptly be named FileWasDeletableByCurrentUser.

Solution 2

Have you tried System.IO.File.GetAccessControl(filename) it should return a FileSecurity with information about the permissions for that file.

Solution 3

Strictly speaking, an UnauthorizedAccessException means that the path is a directory, so you can use a System.IO.Path.GetFileName(path) type command and catch the argument exception.

But if you want a more holistic solution, use System.IO.File.GetAccessControl as mentioned by Dale Halliwell

Solution 4

As stated above. Lookup the file permissions and compare with the user who is running the application.

You could always use this aproach as well

bool deletemyfile()  
{  
    try  
    {  
        ...delete my file  
        return true;  
    }  
    catch  
    {  
        return false;  
    }  
}

if it returns false you know it failed if it returns true then.. it worked and file is gone. Not sure what you're after exactly but this was the best I could think of

Share:
21,892
Dylan Beattie
Author by

Dylan Beattie

Dylan Beattie is a consultant, software developer and international keynote speaker. He has been building data-driven web applications since the 1990s; he's managed teams, taught workshops, and worked on everything from tiny standalone websites to complex distributed systems. He's a Microsoft MVP, and he regularly speaks at conferences and user groups all over the world. Dylan is the creator of the Rockstar programming language, and he's performed his software-themed parodies of classic rock songs all over the world as Dylan Beattie and the Linebreakers.

Updated on January 20, 2020

Comments

  • Dylan Beattie
    Dylan Beattie over 4 years

    In C#, System.IO.File.Delete(filePath) will either delete the specified file, or raise an exception. If the current user doesn't have permission to delete the file, it'll raise an UnauthorizedAccessException.

    Is there some way that I can tell ahead of time whether the delete is likely to throw an UnauthorizedAccessException or not (i.e. query the ACL to see whether the current thread's identity has permission to delete the specified file?)

    I'm basically looking to do:

    if (FileIsDeletableByCurrentUser(filePath)) {
        /* remove supporting database records, etc. here */
        File.Delete(filePath);
    }
    

    but I have no idea how to implement FileIsDeletableByCurrentUser().

  • Dylan Beattie
    Dylan Beattie almost 15 years
    Voted up because your proposed function name is both humorous and insightful.
  • musefan
    musefan about 5 years
    Silly reasoning.... "Is" would be fine because you are checking the permission at that exact point. Otherwise we would have file api functions called "ReadOldData" just in case the data changes between reading it and actually using it.