0

I have a custom file manager, that i wrote in windows forms app (c#).

The idea is to read all of the computers local drives and their contents, put them in a neat treeNode and after that add this node to treeView control.

Since the process of reading the contents took so much time i used

using System.Threading.Tasks;

to implement multithread reading, which looks like so:

void PopulateTreeView() {
    string[] drives = Environment.GetLogicalDrives();
    MyTree.Nodes.Clear();
    foreach (string path in drives) {
        TreeNode rootNode;
        DirectoryInfo info = new DirectoryInfo(path);
        if (info.Exists) {
            rootNode = new TreeNode(info.Name);
            rootNode.Tag = info;
            List<Task> tasks = new List<Task>();
            GetDirectories(info.GetDirectories(), rootNode, tasks);
            MyTree.Nodes.Add(rootNode); 
        }
    }
}

void GetDirectories(DirectoryInfo[] subDirs, TreeNode Parent, List<Task> subTasks) {
    foreach (DirectoryInfo subDir in subDirs) {
        if (subDir.Attributes.HasFlag(FileAttributes.Hidden) ||
            subDir.Attributes.HasFlag(FileAttributes.System)) 
            continue;
        Task t = Task.Run(() => {
            TreeNode rootNode = new TreeNode(subDir.Name, 0, 0);
            rootNode.Tag = subDir;
            rootNode.ImageKey = "folder";
            try { 
                DirectoryInfo[] subSubDirs = subDir.GetDirectories();
                if (subSubDirs.Count() > 0) GetDirectories(subSubDirs, rootNode, new List<Task>());
                Parent.Nodes.Add(rootNode);
            }
            catch (UnauthorizedAccessException) { Parent.Nodes.Add("[Access denied]"); }
            catch (DirectoryNotFoundException) { Parent.Nodes.Add("[Directory missing]"); }
            });
            subTasks.Add(t);
        }
        Task.WaitAll(subTasks.ToArray());
    }

The problem is, program gets stuck here:

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1()); //System.NullReferenceException
    }

And the exceptions' StackTrace shows this:

System.Windows.Forms.TreeNode.Realize(Boolean insertFirst) System.Windows.Forms.TreeNode.Realize(Boolean insertFirst) System.Windows.Forms.TreeNode.Realize(Boolean insertFirst) System.Windows.Forms.TreeNode.Realize(Boolean insertFirst) System.Windows.Forms.TreeView.OnHandleCreated(EventArgs e) System.Windows.Forms.Control.WmCreate(Message& m) System.Windows.Forms.Control.WndProc(Message& m) System.Windows.Forms.TreeView.WndProc(Message& m) System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

I tried to pinpoint the moment it creates "Null Reference". It seems that then I do:

MyTree.Nodes.Add(rootNode);

rootNode has:

-- no Handle

-- no NextNode

-- no PrevNode

all have NullRefExc on them.

Finally, my question is: How do i fix this problem?

P.s. No, this is not the problem stated in NullRefException question. This bug is inconsistent and depends upon the structure of system files. Methods stated in the other thread are not helping me, since no null exceptions appear then I try to pinpoint the moment.

Boreas
  • 13
  • 3

0 Answers0