Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 60,880 times

Related Categories

Listing Files & Folders in a directory

Rollershade

Listing all files in a specified folder

The code below shows how to use the System.IO.DirectoryInfo function to retreive all the files in the specified location, it also show how to get the extension and size aswell. You will have to parse a folder location such as c:\ to the parameter Location. You need one listview1 control

//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);
   }

Listing all folders in a specified folder

This example is similer to the above file routine, but uses dir.GetDirectories to find the folders. In this example you need one treeView control.

//FIND ALL FOLDERS IN FOLDER
TreeNode Main =  treeView1.Nodes.Add("Folders in: " + Location);
Main.Tag = "";
   foreach (System.IO.DirectoryInfo g in dir.GetDirectories())
       {    
       //LOAD FOLDERS
       TreeNode MainNext = Main.Nodes.Add(g.FullName);
       MainNext.Tag = (g.FullName);
       }

I am 25 and live in west Sussex. I enjoy drinking and going out, but I also enjoy writing programs + creating websites in my free time. I play golf and ski.

One of my main intrests when not drinking ;) is programming , the tv can get boring as hell, i first started programming with HTML in 1996 ish and in early 1998 i started learning Visual Basic, i am now learning C#.NET frm Jan 2004. I have continued to learn and gain valuable knowledge from the vb diploma i have completed. While learning vb i became a member of this here Developerfusion.com which i have now become a Moderator at. Examples of my previous projects like WebEdit and EasyCSS can be found on my website.

Comments