1

TreeView 시스템에 디렉토리 목록을 추가하려고하는데 사용자 액세스 문제 인 것 같은 문제가 있습니다. 나는이 문제를 해결하는 데 다양한 단계를 시도했지만 그 중 아무 것도 성공하지 못했습니다. 솔루션 매니페스트 파일의 보안 변경, try catch를 사용하여 액세스 할 수없는 파일을 건너 뛰고 제어를 완료하기 위해 Windows 사용자 폴더 설정 변경 (관리자). 유사한 문제에 대한 답변을 인터넷을 통해 둘러 보았습니다. 대부분의 사람들은 방금 try catch 시스템을 사용했습니다. 모든 것이 그냥 멈추고 그 자리에 앉아 있기 때문에 이것은 내 시스템에서는 작동하지 않습니다. 그런 다음 프로그램은 내 컴퓨터 전체에 하나의 디렉토리가없는 것처럼 작동합니다. 내 코드는 구성TreeView에 디렉토리를 추가 할 때 UnauthorizedAccessException이 발생합니다.

public Folder_Browser() 
    { 
     InitializeComponent(); 
     MapDirectory(); 
    } 

    private void MapDirectory() 
    { 
     TreeNode rootNode; 
     DirectoryInfo dirPrograms = new DirectoryInfo(@"/"); 
     DriveInfo[] loadedDrives = DriveInfo.GetDrives(); 

     foreach (DriveInfo dr in loadedDrives) 
     { 
      if (dr.DriveType != DriveType.Removable) 
      { 
       DirectoryInfo info = new DirectoryInfo(dr.Name); 

       if (info.Exists) 
       { 
        rootNode = new TreeNode(info.Name); 
        rootNode.Tag = info; 
        GetDirectories(info.GetDirectories(), rootNode); 
        treeView1.Nodes.Add(rootNode); 
       } 
      } 
     } 
    } 

    private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo) 
    { 
     TreeNode aNode; 
     DirectoryInfo[] subSubDirs; 

     foreach (DirectoryInfo subDir in subDirs) 

     { 
      aNode = new TreeNode(subDir.Name, 0, 0); 
      aNode.Tag = subDir; 
      aNode.ImageKey = "folder"; 
      try 
      { 
       subSubDirs = subDir.GetDirectories(); 
       //ERROR HERE^^^^^^^ 
       if (subSubDirs != null && subSubDirs.Length != 0) 
       { 
        GetDirectories(subSubDirs, aNode); 
       } 
        nodeToAddTo.Nodes.Add(aNode); 
      } 
      catch (System.UnauthorizedAccessException) 
      { 

      } 

     } 
    } 

나는 문제의이 종류에 다른 사람의 솔루션을 구현하려고했습니다 때마다, 난 그냥 나오는 디렉토리 목록의 어떤 종류를하지 않습니다. 이 프로그램은 너무 많은 리소스를 사용하여 만지지 못하는 폴더를 무시합니다. 간과 한 것이 있습니까? 아니면이 접근법이 가능하지 않습니까? 어떤 도움이라도 고맙게받습니다.

답변

0

이런 종류의 문제가있는 사람은 누구나 내 문제를 해결하는 해결책을 찾을 수있었습니다. 프로그램이 요소 (디렉토리)와 상호 작용하는 방법을 제어하는 ​​추가 기능이 추가되어야합니다. 이 프로그램은 동일한 소스를 사용하여 솔루션에 액세스 할 수없는 디렉터리를 건너 뛰고 (catch catch) System.Security.AccessControl을 사용하여 액세스 할 수있는 디렉터리를 계속 찾을 수 있습니다. 함수는 다음과 같습니다.

using System.Security.AccessControl;

public static void SetAccessRule(string directory) 
    { 
     System.Security.AccessControl.DirectorySecurity Security = System.IO.Directory.GetAccessControl(directory); 
     FileSystemAccessRule accountAllow = new FileSystemAccessRule(Environment.UserDomainName + "\\" + Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow); 
     Security.AddAccessRule(accountAllow); 
    } 
나는 그것에 의해 온 방법이 솔루션에

추가 정보, 그리고에서 찾을 수 있습니다

http://social.msdn.microsoft.com/Forums/vstudio/en-US/206cfa9d-3e5b-43be-840f-49a221e10749/c-unauthorizedaccessexception-when-trying-to-programmically-copying-folder