Question

I'm trying to add a list of directories to a TreeView system, but I'm having issues with what seems to be a user access problem. I've tried various steps in solving this issue, none of which have worked. These include: Changing the security in the solution manifest file, using try catch to skip the files I can't access, and changing my Windows user folder settings to complete control (Administrator). I've looked around throughout the net for answers to similar issues, most people have just used the try catch system. This doesn't work for my system, as everything just freezes up and its sits there. The program then acts like it hasn't found a single directory on my entire computer. My code consists of:

  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)
            {

            }

        }
    }

Every time I've tried implementing someone else's solution to this kind of issue, I just don't get any kind of list of directories coming out. The program takes up too many resources just ignoring the folders it can't touch. Is there something simple I've overlooked? Or is this approach not possible? Any help is appreciated, Cheers.

Was it helpful?

Solution

If anyone has any kind of issue like this, I've managed to find an solution that fixes my problem. An extra function needs to be added, that controls how the program will interact with elements (directories) that it can. This program uses the same source, to skip directories inaccessible to the solution (try catch), but using System.Security.AccessControl allows it to continue and find the directories that can be accessed. The function is as follows:

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

Further information on this solution, and how I came by it, can be found at:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top