
Untitled
By: a guest on
May 5th, 2012 | syntax:
None | size: 1.15 KB | hits: 12 | expires: Never
UnauthorizedAccessException when accessing a directory I just created
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
var a = HasWritePermissionOnDir(dirPath); <- This is only here to see if the value is true and it is.
tw = new StreamWriter(dirPath);
public static bool HasWritePermissionOnDir(string path)
{
var writeAllow = false;
var writeDeny = false;
var accessControlList = Directory.GetAccessControl(path);
if (accessControlList == null)
return false;
var accessRules = accessControlList.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
if (accessRules == null)
return false;
foreach (FileSystemAccessRule rule in accessRules)
{
if ((FileSystemRights.Write & rule.FileSystemRights) != FileSystemRights.Write) continue;
if (rule.AccessControlType == AccessControlType.Allow)
writeAllow = true;
else if (rule.AccessControlType == AccessControlType.Deny)
writeDeny = true;
}
return writeAllow && !writeDeny;
}