Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1400
  • Last Modified:

Building tree-view similar to file explorer... c#

I am following the link below to create file/folder tree-view similar to file explore:
http://msdn.microsoft.com/en-us/library/ms171645%28v=vs.80%29.aspx

For now, I have included only basic exception handling. After I run it, nothing shows up (no folders, no subfolders, no files) in on the form.

Question: How can I make this code work?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace FilesAndFolderInTreeView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            PopulateTreeView();
            this.treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //https://www.youtube.com/watch?v=xSgsEMVy7TU&list=PL1fDI09J3Pb0v32kA9RZyqr6k1gnBnfJq&index=1
            //http://msdn.microsoft.com/en-us/library/ms171645(v=vs.80).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
        }

        private void PopulateTreeView()
        {
            try
            {
                TreeNode rootNode;

                DirectoryInfo info = new DirectoryInfo(@"C:\Users\Public\Documents\UntypeDatasetSample");
                if (info.Exists)
                {
                    rootNode = new TreeNode(info.Name);
                    rootNode.Tag = info;
                    GetDirectories(info.GetDirectories(), rootNode);
                    treeView1.Nodes.Add(rootNode);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("1" + ex.Message);
            }


        }

        private void GetDirectories(DirectoryInfo[] subDirs,
            TreeNode nodeToAddTo)
        {
           
           try
           {
 
            TreeNode aNode;
            DirectoryInfo[] subSubDirs;
            foreach (DirectoryInfo subDir in subDirs)
            {
                aNode = new TreeNode(subDir.Name, 0, 0);
                aNode.Tag = subDir;
                aNode.ImageKey = "folder";
                subSubDirs = subDir.GetDirectories();
                if (subSubDirs.Length != 0)
                {
                    GetDirectories(subSubDirs, aNode);
                }
                nodeToAddTo.Nodes.Add(aNode);
            }
           }
           catch (Exception ex)
           {
               MessageBox.Show("2" + ex.Message);
           }
        }
        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {
            try
            {
            }
            catch (Exception ex)
            {
                MessageBox.Show("3" + ex.Message);
            }
        }
        void treeView1_NodeMouseClick(object sender,
            TreeNodeMouseClickEventArgs e)
        {
            try
            {
            TreeNode newSelected = e.Node;
            listView1.Items.Clear();
            DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
            ListViewItem.ListViewSubItem[] subItems;
            ListViewItem item = null;

            foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
            {
                item = new ListViewItem(dir.Name, 0);
                subItems = new ListViewItem.ListViewSubItem[]
            {new ListViewItem.ListViewSubItem(item, "Directory"), 
             new ListViewItem.ListViewSubItem(item, 
                dir.LastAccessTime.ToShortDateString())};
                item.SubItems.AddRange(subItems);
                listView1.Items.Add(item);
            }
            foreach (FileInfo file in nodeDirInfo.GetFiles())
            {
                item = new ListViewItem(file.Name, 1);
                subItems = new ListViewItem.ListViewSubItem[]
            { new ListViewItem.ListViewSubItem(item, "File"), 
             new ListViewItem.ListViewSubItem(item, 
                file.LastAccessTime.ToShortDateString())};

                item.SubItems.AddRange(subItems);
                listView1.Items.Add(item);
            }

            listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            }
            catch (Exception ex)
            {
                MessageBox.Show("4" + ex.Message);
            }
        }

        private void btnGetFiles_Click(object sender, EventArgs e)
        {
           // PopulateTreeView
        }


    }
}

Open in new window

0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 2
  • 2
2 Solutions
 
käµfm³d 👽Commented:
Are you sure the path exists? I changed your path, and it works fine for me:

Screenshot
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
As you can see, the folder path is correct. It drops "Public" for some reason. Apparently this is harmless because in steping through the code I can see it is able to read subfolder "UntypeDatasetSample".

Also, when I shortened the path to "C:\Users\Public", and because subfolder "My Music" is in "C:\Users\Public", it gives me access denied message despite the fact I have signed in as admin. Furthermore, right-click on "My Music"/Properties/ didn't give me security option to change its access permission settings.

Conclution: (it seems I have two problems)
1. Why doesn't it allow the code to aud certain folders despite the fact I have signed as admin (this item could be handled in another post).

2. Why it doesn't build tree-view for "C:\Users\Public\Documents\UntypeDatasetSample" where it doesn't need access requirements. I understand your test is valid and it works in your test. I will build another form to see if I have managed to mess it up while building my form.

FileAudit
0
 
käµfm³d 👽Commented:
It's due to the behavior of Windows 7:

http://support.microsoft.com/kb/930128

You can't access these "folders"--that's by design. You go through the redirected location(s), which are under the user's profile folder.
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now