Jerry Trac
Jerry Trac

Reputation: 367

List folderName and file contents in listview

Is there a way to list the subfolders and its files in a asp.net listview without using a Sql table?

Thanks

Upvotes: 0

Views: 886

Answers (1)

skub
skub

Reputation: 2306

Assume that you have a listview named ListView1 You can do it with something like the following to list filenames.:

static void ListFiles(string sDir)
   {
       try
       {
           foreach (string d in Directory.GetDirectories(sDir))
           {
               foreach (string f in Directory.GetFiles(d))
               {
                    string fileName = Path.GetFileNameWithoutExtension(f);
                    ListViewItem item = new ListViewItem(fileName);
                    item.Tag =  f; //could get folder name: DirectoryInfo(d).Name


                    ListView1.Items.Add(item);
               }
               ListFiles(d);
           }
       }
       catch (System.Exception ex)
       {
           // handle exceptions here!
       }
   }

Upvotes: 1

Related Questions