Search code examples
c#formsdirectorytreeviewfile-attributes

How do I only view directories containing PDFs with ReadOnly attribute in my C# TreeView?


I have a TreeView in my C# Form and I want it to only display directories containing PDFs that are not Read-Only. The files should also be displayed.

My current code:

private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
    {
        var directoryNode = new TreeNode(directoryInfo.Name);
        foreach (var directory in directoryInfo.GetDirectories())
            directoryNode.Nodes.Add(CreateDirectoryNode(directory));
        foreach (var file in directoryInfo.GetFiles("*.pdf"))
            directoryNode.Nodes.Add(new TreeNode(file.Name));
        return directoryNode;
    }

What I know so far:

  • Checking if it´s Read-Only ((attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly) == true
  • I know that a file can have the following attributes: ReadOnly(R), Hidden(H), Archived(A), System(S),but I´m not sure how to use them.

Thank you in advance.


Solution

  • Your logic has a little flaw in its adding the directory node without checking the existence of any read/write pdf files first.

    Considering there can be cases where a folder contains all read-only pdf files but one of its sub folders contains a read/write pdf file, checking only if there are read/write pdf files in the current directory is also not enough:

    pdffolder
        pdf1 (read-write)
        pdf2 (read-write)
        sub1 (folder)
            pdf3 (read-only)
            sub1-1 (folder)
                pdf4 (read-write)
    

    In this case, we still want sub1 to be visible but pdf3 to be hidden, because we want the sub1-1 folder to be accessible (because there is pdf4 under it)

    Here is your new method. There is only one breaking change, which is, now this method can return NULL if there are no read/write pdf files under it and under any sub folders of it.

    Hope this helps.

    private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
    {
        TreeNode directoryNode = null;
    
        // First, check if there are any read/write files in this folder
        // and create the directory node if yes, and add the pdf file node too.
        foreach (FileInfo file in directoryInfo.GetFiles("*.pdf"))
        {
            if (!File.GetAttributes(file.FullName).HasFlag(FileAttributes.ReadOnly))
            {
                if (directoryNode == null)
                {
                    directoryNode = new TreeNode(directoryInfo.Name);
                }
                directoryNode.Nodes.Add(new TreeNode(file.Name));
            }
        }
    
        // There can be no writeable pdf files, but we should still check
        // the sub directories for the existence of a writeable pdf file
        // because even if this directory does not have one, a sub directory may,
        // and in that case we want that pdf file to be accessible.
        foreach (DirectoryInfo directory in directoryInfo.GetDirectories())
        {
            TreeNode subDirectoryNode = CreateDirectoryNode(directory);
            // the sub directory node could be null if there are no writable
            // pdf files directly under it. Create, if one of the sub directories
            // have one.
            if (subDirectoryNode != null)
            {
                if (directoryNode == null)
                {
                    directoryNode = new TreeNode(directoryInfo.Name);
                }
                directoryNode.Nodes.Add(subDirectoryNode);
            }
        }
    
        // The return value is null only if neither this directory 
        // nor any of its sub directories have any writeable pdf files
        return directoryNode;
    }