2012-04-02 97 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; 
    } 
相關問題