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

Bu makale 183 kere okundu.

Share to Facebook Share to Twitter Share to MySpace Stumble It Share to Reddit Share to Delicious More...

 

   

listing-all-folders-in-a-specified-folder



Linkler



Son Yorumlar

  • Erhan

    tam aradığım konu ama eksik...

  • zuleyha

    burcumu nasil okuyabilirim ...

  • kerem bora

    Maili dün yazmıştım, bugün ...

Twitter