Skip to content

Instantly share code, notes, and snippets.

@hotgazpacho
Created August 4, 2011 15:19
Show Gist options
  • Select an option

  • Save hotgazpacho/1125422 to your computer and use it in GitHub Desktop.

Select an option

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?
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