HOWTO Get full details as why a file can not be deleted or moved. RRS feed

  • Question

  • Please help,

    We are trying to make an automated process that needs to delete and move files. The standard File.Delete/Move is not good enough, as it will not give full details as why a file can not be deleted or moved, when such permission and sharing issues like these below occur:

     - The file has a share lock on it from some process, that prevents it from being deleted.
     - The user (Windows Integrated Authentication user) does not have permission to delete file, specifically.
     - The user does not have permission to delete files from specifically the folder the file is in.
     - The user does not have permission to write to the destination folder when trying to move the file.

    Again, the above are just some possible permission/sharing issues.

    We need a fully detailed account of why any file can not be deleted/moved, at the time we try. The detail must fully detail everything, too include the following:

     - Standard Delete/Move errors.
     - - File does not exist.
     - - Move target folder does not exist.
     - - Move target file name is invalid, such as having invalid characters and/or being too large in length.
     - - - - Too large in length includes if the name + target move folder is too big in length.
     - Sharing lock information.
     - - Which process(es) have a lock on the target file that would otherwise prevent deletion/movement.
     - - The full path to the EXE of the process(es) with the locks.
     - - The user-id running the process(es).
     - The user id being used to try and delete/move the file.
     - Security reasons, why the user can not delete/move the file.
     - - This should include if the permission issue is on the file, the file's folder, and/or the target folder for a move.

    With the above said, the standard C#/.NET File.Delete/Move do not provide any significant detail for sharing/permission issues. In fact, when File.Move can not move a file, and it throws an exception of "UnauthorizedAccessException", the description for File.Delete in MSDN, literally has a list of 4 possible reasons, as why this error may happen, too include READ-ONLY on the file, versus the use in question not having access to delete, and let alone, no detail on the reason to who the user "is" and whether the permission issue is on the file or the folder.

    Another example, if File.Delete can throw "IOException" for 2 reasons, including for when the file is in "use", but again, no details on which app is using it, where that app's EXE resides, or which user the app is running under as. But as an extra twist, the error "UnauthorizedAccessException", says it can occur if the file is an EXE, that is in "use", implying that 2 differenr error exception types could occur for the same reason of a file in "use".

    With the above said, perhaps someone has long since made a solution to deal with C#/.NET and automated processing, to fully capture who, what. why, when, and how deleting/moving files fail. Perhaps even a command line app exists, that Process.Start could invoke.

    Any help would be much appreciated.

    Friday, March 3, 2017 6:57 PM

All replies

  • Don't have an all-in-one answer for you, but I'm recalling the tool called Handle (by SysInternals) which investigates open handles of other processes in the system and this usually will tell you about other processes that may have a lock on the target file.  You could incorporate some of those techniques into your application.  (Not easy.)

    It's clear that what you're looking for doesn't exist in the .NET API though, so I think you're stuck with writing your own code to do the detective work when you encounter an exception.  I don't know if someone has already done this to the degree that you are imagining though, sorry.

    Friday, March 3, 2017 8:42 PM
  • Thanks for the reply Wyck. By chance, do you know at least, a .NET way to move/delete a file that is marked as READ-ONLY, without having to remove the READ-ONLY attribute? The File.Move/Delete commands are documented to throw an error if the READ-ONLY is set.
    Monday, March 6, 2017 3:25 PM
  • I don't know of anything that will do this atomically.  You could strip the read-only flags on source and destination files to delete the destination (if you are expecting to overwrite it) and move the source.  Then you restore the read-only flag on the destination after the move.

    Monday, March 6, 2017 3:42 PM