files and folders in a listview 'SOLVED'

  • 15 years ago

    does anyone know how to fill a listview with files and folders of my local hard disc?

  • 15 years ago

    put one listview and treeview control on a form, ive written this in c# and it may be or may not be what u want as. it first lists all drives in the list, then all folders under that, then listview gets filled with folder location.


    public void FillTree(string Location)
               //CLEAR HOLDERS
               //FIND ALL FILES IN FOLDER
               System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Location);
               foreach (System.IO.FileInfo f in dir.GetFiles("."))
               {    //LOAD FILES
                   ListViewItem lSingleItem = listView1.Items.Add(f.Name);
                   //SUB ITEMS
               //FIND ALL DRIVES
               TreeNode DrivesTop = treeView1.Nodes.Add("Available Drives");
               DrivesTop.ImageIndex = 2;
               DrivesTop.SelectedImageIndex = 2;
               String[] drives = Environment.GetLogicalDrives();
               int NUM = drives.Length;
               int p = 0;
                   String OutPut = Convert.ToString(drives[p]); // + Convert.ToString(drives[p].Length);
                   TreeNode Drives = DrivesTop.Nodes.Add(OutPut);
                   Drives.Tag = Convert.ToString(drives[p]);
                   Drives.ImageIndex = 1;
                   Drives.SelectedImageIndex = 1;
               TreeNode Main =  treeView1.Nodes.Add("Folders in : " + Location);
               Main.Tag = "";
               Main.ImageIndex = 2;
               Main.SelectedImageIndex = 2;
               foreach (System.IO.DirectoryInfo g in dir.GetDirectories())
               {    //LOAD FOLDERS
                   TreeNode MainNext = Main.Nodes.Add(g.FullName);
                   MainNext.Tag = (g.FullName);
               //EXPAND TREE + GOTO TOP

    now to call this , do


    string Location = Convert.ToString(txtPath.Text);

    and to change the listview files when folder,drive is clicked use this


    private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
       if (Convert.ToString(treeView1.SelectedNode.Tag) == "")
       {//DO NOWT
           txtPath.Text = Convert.ToString(treeView1.SelectedNode.Tag);
           string Location = Convert.ToString(treeView1.SelectedNode.Tag);

    now as u posted in VB.NET and ive given code for c# , u can use this converter to change the code

    also u can remove the Main.ImageIndex = 2; + Main.SelectedImageIndex = 2; from the code as i have an imagelist associated with the treeview.

  • 11 years ago

    It's been a long time since this was posted but I'm tying to implement it in C# Express and get a couple of errors:

    (1) FillTree(Location); Method must have a return type; (2} Identifier expected

    (3) The name 'txtPath' does not exist in the current context

    Would greatly appreciate feedback.


Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” - Donald Knuth