Tutorials Forums
     Tutorials Videos
        Sign Up Now For FREE
Welcome, Guest
Username Password: Remember me

VS2008 Get all files into a datatable extension
(1 viewing) (1) Guest
VB.NET HELP, VB.NET TUTORIALS, VB.NET CODE, VB.NET PROGRAMMING, VB.NET

Visual Basic.NET is the most recent generation of Visual Basic. Developers will be pleased to note that its new features include inheritance, method overloading, structured exception handling, and more. These capabilities make it easier than ever to create .NET applications, including Windows applications, web services, and web applications. The articles in this section give you all the tips you need to work wit this useful language.
  • Page:
  • 1

TOPIC: VS2008 Get all files into a datatable extension

VS2008 Get all files into a datatable extension 09 Dec 2009 15:30 #71

Extension method to return all files in a specific folder into a DataTable. From here you can iterate thru code, show in a Listbox, DataGridView etc.

Instructions: Copy code into a code module then try the following simple demo.

Please note there are different paths you can take with this so feel free to hack away :-)

Dim dt As New DataTable
dt = "C:\".GetFiles
ListBox1.DataSource = dt
ListBox1.DisplayMember = "FileName"
ListBox1.ValueMember = "Modified"


AddHandler _
.ListBox1.SelectedIndexChanged, _
AddressOf .ListBox1_SelectedIndexChanged
.ListBox1_SelectedIndexChanged(.ListBox1, Nothing)

Public Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles ListBox1.SelectedIndexChanged
Label2.Text = String.Format("{0} was last modified on {1}", ListBox1.Text, ListBox1.SelectedValue)
End Sub

 
''' <summary>
'
'' Fills a DataTable with files contained in path
''' </summary>
'
'' <param name="path">Location to get files</param>
''' <returns></returns>
'
'' <remarks></remarks>
<Extension()> _
Public Function GetFiles(ByVal path As String) As DataTable
Dim Table As New DataTable
 
If IO.Directory.Exists(path) Then
Dim Row As DataRow
 
Table.Columns.Add("FileName", GetType(System.String))
Table.Columns.Add("Length", GetType(System.Int64))
Table.Columns.Add("Modified", GetType(System.DateTime))
 
For Each FilePath As String In My.Computer.FileSystem.GetFiles(path)
Row = Table.NewRow
Row("FileName") = My.Computer.FileSystem.GetFileInfo(FilePath).Name
'Console.WriteLine(My.Computer.FileSystem.GetFileInfo(FilePath).DirectoryName)
Row("Length") = My.Computer.FileSystem.GetFileInfo(FilePath).Length
Row("Modified") = My.Computer.FileSystem.GetFileInfo(FilePath).LastWriteTime
Table.Rows.Add(Row)
Next
 
End If
 
Return Table
 
End Function
 
 
  • CE
  • OFFLINE
  • Administrator
  • Posts: 197
  • Karma: 78
CodersEngine
  • Page:
  • 1
Time to create page: 0.37 seconds