Created
August 4, 2011 15:19
-
-
Save hotgazpacho/1125422 to your computer and use it in GitHub Desktop.
Is this a reasonable way to check if the user has write access to the given path?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| bool CheckWriteAccessToDirectory(string directoryPath) | |
| { | |
| bool isWriteAccess = false; | |
| try | |
| { | |
| var collection = Directory.GetAccessControl(directoryPath).GetAccessRules(true, true, typeof(NTAccount)); | |
| var fileSystemAccessRules = collection.Cast<FileSystemAccessRule>().ToList(); | |
| isWriteAccess = fileSystemAccessRules.Any(rule => rule.AccessControlType == AccessControlType.Allow) | |
| && fileSystemAccessRules.None(rule => rule.AccessControlType == AccessControlType.Deny); | |
| } | |
| catch (UnauthorizedAccessException) | |
| { | |
| isWriteAccess = false; | |
| } | |
| catch (Exception) | |
| { | |
| isWriteAccess = false; | |
| } | |
| return isWriteAccess; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment