How to update a TreeView of directories in C #

2

I have a TreeViewMS component (the one with Multiple Selection) listing the system directories on a Form.

I created an event on the Form that when pressed F5, this TreeView will refresh (if some new directory on the system or not).

It even gets to update normally, so a NullReferenceException is thrown in TreeView MS.dll in the program's initial class (Program.cs)

Following code to load the TreeView.

private void LoadTreeView() {
   tvDirectories.Nodes.Clear();
   exArq.CreateDirectoryTree(tvDirectories);
}

Here's the Excess:

An unhandled exception of type 'System.NullReferenceException' occurred in TreeViewMS.dll Additional information: Referência de objeto não definida para uma instância de um objeto.

CreateDirectoryTree method searches the system directories and mounts the tree, I believe it is not the problem but follows the method:

public TreeViewMS.TreeViewMS CreateDirectoryTree(TreeViewMS.TreeViewMS treeView) {

    foreach (DriveInfo drv in DriveInfo.GetDrives()) {
        TreeNode rootDirectoryNode = new TreeNode();
        rootDirectoryNode.ImageIndex = ImageIndexs.SystemDiskIcon;
        rootDirectoryNode.SelectedImageIndex = ImageIndexs.SystemDiskIcon;
        rootDirectoryNode.Text = drv.Name;

        switch (drv.DriveType) {

            case DriveType.Network:
                rootDirectoryNode.ImageIndex = ImageIndexs.NetworkDiskIcon;
                rootDirectoryNode.SelectedImageIndex = ImageIndexs.NetworkDiskIcon;
                break;

            case DriveType.CDRom:
                rootDirectoryNode.ImageIndex = ImageIndexs.CDDiskIcon;
                rootDirectoryNode.SelectedImageIndex = ImageIndexs.CDDiskIcon;
                break;

            case DriveType.Fixed:
                rootDirectoryNode.ImageIndex = ImageIndexs.LocalDiskIcon;
                rootDirectoryNode.SelectedImageIndex = ImageIndexs.LocalDiskIcon;
                break;

            case DriveType.Removable:
                rootDirectoryNode.ImageIndex = ImageIndexs.RemovalDiskIcon;
                rootDirectoryNode.SelectedImageIndex = ImageIndexs.RemovalDiskIcon;
                break;

        }

        bool isSystemDirectory = drv.Name.ToString() == Path.GetPathRoot(Environment.SystemDirectory);

        if (isSystemDirectory) {
            rootDirectoryNode.ImageIndex = ImageIndexs.SystemDiskIcon;
            rootDirectoryNode.SelectedImageIndex = ImageIndexs.SystemDiskIcon;
        }

        rootDirectoryNode.Nodes.Add(String.Empty);

        treeView.Nodes.Add(rootDirectoryNode);
    }
    return treeView;

}        

I suspect it's the way to clear the TreeView that should be causing the error.

I imagine it should not be, but I will also include where the node is expanded:

private void tvDiretorios2_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
     TreeNode newNode = exArq.GetDirectoriesAndFilesNodes(e.Node);
}

and the function that these directories are looking for

public TreeNode GetDirectoriesAndFilesNodes(TreeNode parentNode) {

    string nodePath = parentNode.FullPath + Constants.DIRECTORY_SEPARATOR;

    DirectoryInfo rootDirectory = new DirectoryInfo(nodePath);
    parentNode.Nodes[0].Remove();
    try {

        foreach (DirectoryInfo dir in rootDirectory.GetDirectories()) {

            TreeNode directoryNode = new TreeNode();
            directoryNode.Text = dir.Name;
            directoryNode.Nodes.Add(String.Empty);
            directoryNode.ImageIndex = ImageIndexs.DirectoryIcon;
            directoryNode.SelectedImageIndex = ImageIndexs.DirectoryIcon;
            parentNode.Nodes.Add(directoryNode);

        }

        foreach (FileInfo file in rootDirectory.GetFiles()) {

            bool isExcel = Path.GetExtension(file.Name).ToLower().Equals(Constants.EXTENSION_EXCEL_X) || Path.GetExtension(file.Name).ToLower().Equals(Constants.EXTENSION_EXCEL);

            bool isZipedFile = Path.GetExtension(file.Name).ToLower().Equals(Constants.EXTENSION_RAR) || Path.GetExtension(file.Name).ToLower().Equals(Constants.EXTENSION_ZIP);

            bool isTextFile = Path.GetExtension(file.Name).ToLower().Equals(Constants.EXTENSION_TXT);

            TreeNode fileNode = new TreeNode();
            fileNode.Text = file.Name;

            if (isExcel) {
                fileNode.ImageIndex = ImageIndexs.ExcelFileIcon;
                fileNode.SelectedImageIndex = ImageIndexs.ExcelFileIcon;
            } else if (isZipedFile) {
                fileNode.ImageIndex = ImageIndexs.ZipedFileIcon;
                fileNode.SelectedImageIndex = ImageIndexs.ZipedFileIcon;
            } else if (isTextFile) {
                fileNode.ImageIndex = ImageIndexs.TextFileIcon;
                fileNode.SelectedImageIndex = ImageIndexs.TextFileIcon;
            } else {
                fileNode.ImageIndex = ImageIndexs.GenericFileIcon;
                fileNode.SelectedImageIndex = ImageIndexs.GenericFileIcon;
            }

            parentNode.Nodes.Add(fileNode);
        }

    } catch (UnauthorizedAccessException) {
        TreeNode errorNode = new TreeNode();
        errorNode.Text = "Acesso Negado";
        errorNode.ImageIndex = ImageIndexs.NoPermissionIcon;
        errorNode.SelectedImageIndex = ImageIndexs.NoPermissionIcon;
        parentNode.Nodes.Add(errorNode);
    } catch (IOException) {
        TreeNode errorNode = new TreeNode();
        errorNode.Text = "O Dispositivo não está pronto";
        errorNode.ImageIndex = ImageIndexs.DriverNotReady;
        errorNode.SelectedImageIndex = ImageIndexs.DriverNotReady;
        parentNode.Nodes.Add(errorNode);
    }

    return parentNode;
} 

Thank you to anyone who can help.

    
asked by anonymous 25.04.2016 / 16:29

1 answer

1

Guilherme, to help you identify where the error is, you can include in your codes a try ... catch, so you can identify where the error is. You can put a breakpoint on the throw.

Using the breakpoint you can evaluate the entire string of the exception and see where the error is.

private void LoadTreeView()
{
    try
    {
        tvDirectories.Nodes.Clear();
        exArq.CreateDirectoryTree(tvDirectories);
    }
    catch (Exception ex)
    {
        throw;
    }
}
    
21.11.2016 / 12:03