DevPinoy.org
A Filipino Developers Community
   
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.

[code language="C#"]

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

[/code]

Sample usage would be as follows:

[code language="C#"]

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

[/code]

or

[code language="C#"]

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

[/code]

*update:  Here's the VB.NET version as requested

[code language="VB.NET"]

Imports System
Imports System.Data
Imports System.IO
Namespace KeithRull.Utilities

 Public NotInheritable Class FileSytem

   Private Sub New()
   End Sub

   Public Shared Function FileSystemToDataTable(ByVal directory As String) As DataTable

     Dim directoryInfo As DirectoryInfo = New DirectoryInfo(directory)

     Dim filesystem As DataTable = New DataTable

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

     For Each fileSystemInfo As FileSystemInfo In directoryInfo.GetFileSystemInfos
       Dim dataRow As DataRow = filesystem.NewRow
       dataRow("Name") = fileSystemInfo.Name
       dataRow("FullName") = fileSystemInfo.FullName
       dataRow("CreationTime") = fileSystemInfo.CreationTime
       dataRow("LastWriteTime") = fileSystemInfo.LastWriteTime
       filesystem.Rows.Add(dataRow)
     Next

     Return filesystem

   End Function

 End Class

End Namespace

[/code]


Posted 05-12-2006 6:25 PM by keithrull
Filed under:

Comments

Orange wrote re: How To: Load The FileSystem Into A DataTable
on 05-13-2006 3:32 AM
Hindi ko kayang i-translate yan ehh. pede ba yan sa VB?
keithrull wrote re: How To: Load The FileSystem Into A DataTable
on 05-13-2006 8:51 AM
Oops! sorry about that. here's the VB.NET Version:

Imports System
Imports System.Data
Imports System.IO
Namespace KeithRull.Utilities

Public NotInheritable Class FileSytem

  Private Sub New()
  End Sub

  Public Shared Function FileSystemToDataTable(ByVal directory As String) As DataTable
    Dim directoryInfo As DirectoryInfo = New DirectoryInfo(directory)
    Dim filesystem As DataTable = New DataTable
    filesystem.Columns.Add(New DataColumn("Name"))
    filesystem.Columns.Add(New DataColumn("FullName"))
    filesystem.Columns.Add(New DataColumn("CreationTime"))
    filesystem.Columns.Add(New DataColumn("LastWriteTime"))
    For Each fileSystemInfo As FileSystemInfo In directoryInfo.GetFileSystemInfos
      Dim dataRow As DataRow = filesystem.NewRow
      dataRow("Name") = fileSystemInfo.Name
      dataRow("FullName") = fileSystemInfo.FullName
      dataRow("CreationTime") = fileSystemInfo.CreationTime
      dataRow("LastWriteTime") = fileSystemInfo.LastWriteTime
      filesystem.Rows.Add(dataRow)
    Next
    Return filesystem
  End Function
End Class
End Namespace
rdagumampan wrote re: How To: Load The FileSystem Into A DataTable
on 05-14-2006 9:35 PM
bro if for databinding we can simply use this

DirectoryInfo di = new DirectoryInfo("C:\\DataFolder");
FileInfo[] files = di.GetFileSystemInfos();

dataGrid.DataSourrce = files;
dataGrid.DataBind();
keithrull wrote re: How To: Load The FileSystem Into A DataTable
on 05-14-2006 11:16 PM
true. i wrote that same point in an article i wrote at code project more than a year ago.

the idea behind this how to is just to show things that you can do with the data table and the file system. this howto lays down the foundation to my next article :)
Orange wrote re: How To: Load The FileSystem Into A DataTable
on 05-15-2006 11:17 PM
Uy... okies.
John wrote re: How To: Load The FileSystem Into A DataTable
on 04-20-2008 9:44 AM

Hey, this is great, exactly what I was just getting ready to put together.  Great work man!

cgoasduff wrote re: How To: Load The FileSystem Into A DataTable
on 05-14-2008 7:18 AM

Thanks a lot for sharing this Keith. It works great.

Do you know a way to sort the display by "LastWriteTime" by any chance ?

Currently it displays by alphabetical order of the file name.

Thanks

Chris

cgoasduff wrote re: How To: Load The FileSystem Into A DataTable
on 05-15-2008 1:57 AM

Ignore my previous message. It's now working by creating and sorting a dataview and connecting my datagrid to the dataview

 DataView dv = new DataView(KeithRull.Utilities.FileSytem.FileSystemToDataTable(directory));

       dv.Sort = "LastWriteTime DESC";

       // connect and bid the datagrid (articleList) to the Dataview (dv)

       articleList.DataSource = dv;

       articleList.DataBind();

Copyright DevPinoy 2005-2008