0

I have a TreeView that maps and populates all the folders in a directory, I'm passing in the variable virtualPath

Is there is any function or property that can tell me if the directory is blocked or not blocked? So that I can show a message to the user saying that he does not have privileges to access the directory.

String[] directories= Listdirectories(virtualPath.ToString());

foreach (string directory in directories) {
    node = new RadTreeNode(Path.GetDirectoryName(directory .ToString()));
    node.Value = virtualPath + "\\" + Path.GetFileName(directory .ToString());
    parentNode.Nodes.Add(node);
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Kinda the same as this: http://stackoverflow.com/questions/130617/how-do-you-check-for-permissions-to-write-to-a-directory-or-file – CAbbott Apr 02 '12 at 15:50

2 Answers2

2

Directory.GetAcessControl(path) does what you are asking for.

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;
}

(FileSystemRights.Write & rights) == FileSystemRights.Write is using something called "Flags" btw which if you don't know what it is you should really read up on :)

TOP KEK
  • 2,593
  • 5
  • 36
  • 62
1

While populating Treeview Tag the required nodes with : node.Tag="LOCK"

Then use

    private void trv_SourceFolder_BeforeExpand(object sender, TreeViewCancelEventArgs e)
    {
        if (Convert.ToString(e.Node.Tag) == "LOCK")
            e.Cancel = true;
    }