如何检查目录是否在TreeView中被锁定?

时间:2012-04-02 15:40:35

标签: c# asp.net

我有TreeView映射并填充目录中的所有文件夹,我传入变量virtualPath

是否有任何功能或属性可以告诉我目录是否被阻止或被阻止? 这样我就可以向用户显示一条消息,说他没有访问该目录的权限。

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

2 个答案:

答案 0 :(得分:1)

Directory.GetAcessControl(path)可以满足您的要求。

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正在使用名为“Flags”btw的东西,如果你不知道它应该是什么,你应该真正阅读:)

答案 1 :(得分:1)

在填充Treeview时标记所需的节点: node.Tag = “LOCK”

然后使用

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