?
Solved

Display a list of files from a directory in a datagridview

Posted on 2008-10-27
5
Medium Priority
?
1,877 Views
Last Modified: 2012-06-27
Hello,

I am trying to display all the files in a network share folder which are of type: .doc .xls .pdf
The code im trying to use is pasted in the code snippet box. But it is adapted from this:


       Dim dt As New DataTable  
        dt.Columns.Add("File Name")  
        dt.Columns.Add("File Path")  
 
        'Harvest the file paths  
        Dim files As New List(Of String)  
        files.AddRange(System.IO.Directory.GetFiles(System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "*.jpg", IO.SearchOption.AllDirectories))  
        files.AddRange(System.IO.Directory.GetFiles(System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "*.gif", IO.SearchOption.AllDirectories))  
        files.AddRange(System.IO.Directory.GetFiles(System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "*.png", IO.SearchOption.AllDirectories))  
        files.AddRange(System.IO.Directory.GetFiles(System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "*.bmp", IO.SearchOption.AllDirectories))  
        files.AddRange(System.IO.Directory.GetFiles(System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "*.tif", IO.SearchOption.AllDirectories))  
 
        'Load the temp table  
        For Each file As String In files  
            dt.Rows.Add(New Object() {System.IO.Path.GetFileName(file), file})  
        Next  
 
        'Bind to the result  
        Me.BindingSource1.DataSource = dt  

'Setup the temp table   
        Dim dt As New DataTable
        dt.Columns.Add("File Name")
        dt.Columns.Add("File Path")
 
        'Harvest the file paths   
        Dim files As New List(Of String)
        files.AddRange(System.IO.Directory.GetFiles(System.Environment.GetFolderPath("\\MYFINSERV\MY_OFFICE\CLIENTS\HISTORY"), "*.doc", IO.SearchOption.AllDirectories))
        files.AddRange(System.IO.Directory.GetFiles(System.Environment.GetFolderPath("\\MYFINSERV\MY_OFFICE\CLIENTS\HISTORY"), "*.docx", IO.SearchOption.AllDirectories))
        files.AddRange(System.IO.Directory.GetFiles(System.Environment.GetFolderPath("\\MYFINSERV\MY_OFFICE\CLIENTS\HISTORY"), "*.pdf", IO.SearchOption.AllDirectories))
        files.AddRange(System.IO.Directory.GetFiles(System.Environment.GetFolderPath("\\MYFINSERV\MY_OFFICE\CLIENTS\HISTORY"), "*.xls", IO.SearchOption.AllDirectories))
        files.AddRange(System.IO.Directory.GetFiles(System.Environment.GetFolderPath("\\MYFINSERV\MY_OFFICE\CLIENTS\HISTORY"), "*.ico", IO.SearchOption.AllDirectories))
        files.AddRange(System.IO.Directory.GetFiles(System.Environment.GetFolderPath("\\MYFINSERV\MY_OFFICE\CLIENTS\HISTORY"), "*.png", IO.SearchOption.AllDirectories))
 
        'Load the temp table   
        For Each file As String In files
            dt.Rows.Add(New Object() {System.IO.Path.GetFileName(file), file})
        Next
 
        'Bind to the result   
        Me.BindingSourceDocs.DataSource = dt
 
        Me.historyDocs.AutoGenerateColumns = True
        Me.historyDocs.DataSource = Me.BindingSourceDocs

Open in new window

0
Comment
Question by:drews1f
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:jorgesv13
ID: 22815554
Okay, what's your specific problem?
are you getting an error or something?
0
 

Author Comment

by:drews1f
ID: 22815611
yes i was string cannot be transfered into integer or something but ive managed to fix it.
code attached incase anyones wondering :0

Only slight problem i still have is the code will only show the files in that specific dir and not from any dirs beneath it. ie

MY_OFFICE\
                   |  1.JPG

MY_OFFICE\CLIENTS\HISTORY\139187
                                                                |   2.JPG
                                                                |   3.JPG

if the dir was to just MY_OFFICE\ then i would only get 1.JPG and not the other 2 files.
How can i adjust my code to capture the sub dirs?

Many thanks
        Dim dirPath As String
        dirPath = "\\myfinserv\MY_OFFICE\CLIENTS\HISTORY\139187"
        'Setup the temp table   
        Dim dt As New DataTable
        dt.Columns.Add("File Name")
        dt.Columns.Add("File Path")
 
        'Harvest the file paths   
        Dim files As New List(Of String)
        files.AddRange(Directory.GetFiles(dirPath, "*.ico"))
        files.AddRange(Directory.GetFiles(dirPath, "*.png"))
 
 
        'Load the temp table   
        For Each file As String In files
            dt.Rows.Add(New Object() {System.IO.Path.GetFileName(file), System.IO.File.GetCreationTime(file)})
        Next
 
        'Bind to the result   
        Me.BindingSourceDocs.DataSource = dt
 
        Me.historyDocs.AutoGenerateColumns = True
        Me.historyDocs.DataSource = Me.BindingSourceDocs

Open in new window

0
 
LVL 8

Accepted Solution

by:
jorgesv13 earned 2000 total points
ID: 22815640
Just add the "AllDirectories" modifier:
 files.AddRange(Directory.GetFiles(dirPath, "*.ico",SearchOption.AllDirectories))
        files.AddRange(Directory.GetFiles(dirPath, "*.png",SearchOption.AllDirectories))

Open in new window

0
 

Author Comment

by:drews1f
ID: 22815654
lol yeh just figured that out!!! duhhhhh!!!! im so new!
thanks a bundle anyway mate :)
hope the code here helps someone cause its taken me 2 hours to get right!!
0
 
LVL 8

Expert Comment

by:jorgesv13
ID: 22815659
But you might want to validate that the file string is indeed a file and not a directory:
For Each file As String In files
if not directory.Exists(file) then
            dt.Rows.Add(New Object() {System.IO.Path.GetFileName(file), System.IO.File.GetCreationTime(file)})
end if
        Next

Open in new window

0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question