2012-04-02 68 views
0

我有一个TreeView映射并填充目录中的所有文件夹,我传递的变量virtualPath如何检查目录是否被锁定在TreeView中?

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

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

有点儿与此相同:http://stackoverflow.com/questions/1 30617 /怎么办,你检查换权限对写入到一个目录,或文件 – CAbbott 2012-04-02 15:50:19

回答

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 &权)== FileSystemRights.Write是使用所谓的“标志” BTW它,如果你不知道它是什么你应该阅读了关于:)

1

虽然填充树形目录标签具有所需节点: node.Tag = “LOCK”

然后使用

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