How To: Load The FileSystem Into A DataTable#

A quick and easy way to load files and directories contained in a specified folder into a DataTable.

using System;
using System.Data;
using System.IO;

namespace KeithRull.Utilities
{
   /// <summary>
   /// A collection of methods that handles the processing of FileSystem data and information
   /// </summary>
   public sealed class FileSytem
   {
      private FileSytem(){}

      /// <summary>
      /// A method that accepts a string that points to a directory, 
      /// reads that folders contents and convert that data into a DataTable
      /// </summary>
      /// <param name="directory">the directory to browse</param>
      /// <returns>a DataTable containing the information about the supplied directory</returns>
      public static DataTable FileSystemToDataTable(string directory)
      {
         //read the folder 
         DirectoryInfo directoryInfo = new DirectoryInfo(directory);

         //create our datatable that would hold the list
         //of folders in the specified directory
         DataTable filesystem = new DataTable();

         /* Add the columns to our datatable
         * You can add as many colums as you like.
         * for this demo, we will just be adding this columns
         * */

         filesystem.Columns.Add(new DataColumn("Name"));
         filesystem.Columns.Add(new DataColumn("FullName"));
         filesystem.Columns.Add(new DataColumn("CreationTime"));
         filesystem.Columns.Add(new DataColumn("LastWriteTime"));

         //loop thru each FileSystemInfo object in the specified directory
         foreach (FileSystemInfo fileSystemInfo in directoryInfo.GetFileSystemInfos())
         {
            //create a new row in ould filesystem table
            DataRow dataRow = filesystem.NewRow();

            //assign the values to our table members
            dataRow["Name"] = fileSystemInfo.Name;
            dataRow["FullName"] = fileSystemInfo.FullName;
            dataRow["CreationTime"] = fileSystemInfo.CreationTime;
            dataRow["LastWriteTime"] = fileSystemInfo.LastWriteTime;

            // add the datarow to our datatable
            filesystem.Rows.Add(dataRow);
         }

         //return our table
         return filesystem;
      }
   }
}

Sample usage would be as follows:

string directory = Server.MapPath(""); //load a specific path in our web server
DataGrid1.DataSource = KeithRull.Utilities.FileSytem.FileSystemToDataTable(directory);
DataGrid1.DataBind();

or

string directory = "c:\"; //just point to a folder in the machine
DataGrid1.DataSource = KeithRull.Utilities.FileSytem.FileSystemToDataTable(directory);
DataGrid1.DataBind();

Saturday, May 13, 2006 2:30:06 AM (GMT Daylight Time, UTC+01:00) #    Comments [0]  | 

 

Comments are closed.
All content © 2010, Keith Rull
On this page
This site
Calendar
<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 2.3.9074.18820

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: