Solved

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

Posted on 2014-12-25
4
808 Views
Last Modified: 2014-12-25
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
Comment
Question by:Mike Eghtebas
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40517759
Are you sure the path exists? I changed your path, and it works fine for me:

Screenshot
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40517832
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
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 500 total points
ID: 40517886
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
 
LVL 33

Author Closing Comment

by:Mike Eghtebas
ID: 40517935
Thank you.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now