Introduction
The use of the .NET Framework DirectoryInfo() class is great. It allows to collect data information about the files in a directory. However, its use is not that easy. This is not data driven and, very often, having such a flexibility would be greatly welcome. In this article, I present a small class to read directory files which allows provide a few interesting goodies.
The basis
Getting the content of a directory can be as simple as:
Dim loDirectory As New DirectoryInfo(tcDirectory)
For Each loFile In loDirectory.GetFiles("*.*") Next
A higher level approach
When I built the Level Extreme .NET Framework, my perception was that many of those class should be encapsulated into higher level classes. Basically, by keeping the data driven aspect in mind, getting the content of a directory into a dataset should be as simple as:
Dim loFileDirectory As Framework.FileDirectory = New Framework.FileDirectory(oProcess) loFileDirectory.cDirectory = "d:\MyDirectory" loFileDirectory.GetFile() Return loFileDirectory.oDataSet
How does it work?
The sequence of operations to obtain the content of a directory or multiple directories into a dataset is really simple. For each directory you wish to collect the content from, simply set the cDirectory property and issue a call the GetFile() method. This will accumulate the data of each directory into the dataset.
The class supports, by default, the addition of the directory column in the data set. This is useful when you want to gather the content of various directories into one data set. Thus, it is then necessary to have the directory column in order to know to which directory the file belongs. If you only work with one directory, simply set lDirectory to False if you do not wish to have this column.
The class
Here is the source code of the class:
Imports System.IO Public Class FileDirectory Public cDirectory As String = "" Public cFilter As String = "*.*" Public lDirectory As Boolean = True Public oDataSet As DataSet = New DataSet Public oDataView As DataView = New DataView Private lCreateDataSet As Boolean = False Private oApp As Framework.App Private oProcess As Framework.LXProcess ' This is when we access the data provider in desktop and Web service mode Sub New(ByVal toApplication As Framework.App) oApp = toApplication End Sub ' This is when we access the data provider in a Web mode Public Sub New(ByVal toProcess As Framework.LXProcess) oProcess = toProcess oApp = oProcess.oApp End Sub ' Create the dataset Public Function CreateDataSet() As Boolean oDataSet.Tables.Add("Temp") ' If we include the directory If lDirectory Then oDataSet.Tables(0).Columns.Add("Directory", GetType(System.String)) End If oDataSet.Tables(0).Columns.Add("Extension", GetType(System.String)) oDataSet.Tables(0).Columns.Add("LastUpdate", GetType(System.DateTime)) oDataSet.Tables(0).Columns.Add("Name", GetType(System.String)) oDataSet.Tables(0).Columns.Add("Size", GetType(System.Int32)) oDataSet.Tables(0).Columns("Name").MaxLength = 100 ' Assign the value so we know that the creation of the data set will only be done once lCreateDataSet = True End Function ' Add the files from a directory into the dataset Public Function GetFile() As Boolean Dim loDirectory As New DirectoryInfo(cDirectory) Dim loFile As FileInfo ' See if we have to create the dataset If Not lCreateDataSet Then CreateDataSet() End If ' Get all the files in the directory For Each loFile In loDirectory.GetFiles(cFilter) Dim loRow As DataRow = oDataSet.Tables(0).NewRow ' If we include the directory If lDirectory Then loRow("Directory") = oApp.RemoveBackSlash(cDirectory) End If loRow("Extension") = loFile.Extension loRow("LastUpdate") = loFile.LastWriteTime loRow("Name") = loFile.Name loRow("Size") = loFile.Length oDataSet.Tables(0).Rows.Add(loRow) Next End Function ' Sort the dataset ' expC1 Field Public Function Sort(ByVal tcField As String) As Boolean oDataView = oDataSet.Tables(0).DefaultView oDataView.Sort = tcField Return True End Function End Class
A few notes
When an instance of this class is created, a oProcess object is passed. This is simply my highest object of the context, in this case the Web site hit.
There is a Sort() method available if you wish to sort the dataset once created. You simply need to pass the field name in which you wish to sort the dataset on. The Sort() method should be called at the end of the process, thus once the dataset is fully created.
This class only collects some basic information on each file. Of course, you can easily adapt it to add more types of information in regards to each file.
The class includes a cFilter property which can be used to assign the filter in which you want to apply the content of the directory on. By default, all files will be returned.