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.


    Code:

    public void FillTree(string Location)
           {
               //CLEAR HOLDERS
               treeView1.Nodes.Clear();
               listView1.Items.Clear();
               //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
                   lSingleItem.SubItems.Add(Convert.ToString(f.Length));
                   lSingleItem.SubItems.Add(f.Extension);
                   }
               //FIND ALL DRIVES
               TreeNode DrivesTop = treeView1.Nodes.Add("Available Drives");
               DrivesTop.ImageIndex = 2;
               DrivesTop.SelectedImageIndex = 2;
               DrivesTop.Tag="";
               String[] drives = Environment.GetLogicalDrives();
               int NUM = drives.Length;
               int p = 0;
               while(p!=NUM)
               {
                   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;
                   p++;
               }
               
               //FIND ALL FOLDERS IN FOLDER
               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
               treeView1.ExpandAll();
           }


    now to call this , do


    Code:

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


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


    Code:

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


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


    http://www.developerfusion.com/utilities/convertcsharptovb.aspx


    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.

    Thanks.

Post a reply

Enter your message below

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

Contribute

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