How to list multiple file types with this code vb.net aspx

I found this code from the asp.net guys and would like to list the files in a folder with multiple file types, for example I would like to list .iso, .exe, .zip and not .aspx files. Can anyone help with this. I have been searching around but I am not a good enough coder to figure this out right now.

### Code Below


Sub Page_Load(sender as Object, e as EventArgs)
    Dim dirInfo as New DirectoryInfo(Server.MapPath(""))
   
    articleList.DataSource = dirInfo.GetFiles("*.iso")
    articleList.DataBind()
  End Sub

</script>


<asp:DataGrid runat="server" id="articleList" Font-Name="Verdana"
      AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee"
      HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White"
      HeaderStyle-Font-Size="8pt" HeaderStyle-Font-Bold="True">
  <Columns>
    <asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name" HeaderText="File Name" />
    <asp:BoundColumn DataField="LastWriteTime" HeaderText="Last Write Time" ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" />
    <asp:BoundColumn DataField="Length" HeaderText="File Size" ItemStyle-HorizontalAlign="Right" DataFormatString="{0:#,### bytes}" />
  </Columns>
</asp:DataGrid>  
ngranickAsked:
Who is Participating?
 
SriVaddadiConnect With a Mentor Commented:
Dim dir As New DirectoryInfo(Server.MapPath("~"))
        Dim isoFiles() As FileInfo
        Dim zipFiles() As FileInfo
        Dim exeFiles() As FileInfo
        Dim aspxFiles() As FileInfo
        Dim allFiles() As FileInfo

        isoFiles = dir.GetFiles("*.iso")
        zipFiles = dir.GetFiles("*.zip")
        exeFiles = dir.GetFiles("*.exe")
        aspxFiles = dir.GetFiles("*.aspx")
        ReDim allFiles(isoFiles.Length + zipFiles.Length + exeFiles.Length + aspxFiles.Length)
        isoFiles.CopyTo(allFiles, 0)
        zipFiles.CopyTo(allFiles, isoFiles.Length)
        exeFiles.CopyTo(allFiles, isoFiles.Length + zipFiles.Length)
        aspxFiles.CopyTo(allFiles, isoFiles.Length + zipFiles.Length + exeFiles.Length)

articleList.DataSource = allfiles
articleList.DataBind()
0
 
ngranickAuthor Commented:
Fantastic. worked like a champ. Thanks for the superfast response!
0
 
ngranickAuthor Commented:
This code works great. Only issue I have found is that when there are no files in the folder of the files types specified the aspx page errors out and this is what it gives back.

Any idea how to not error out this hard and show the web page even when there is no files in the folder on the web server?


Server Error in '/' Application.
A field or property with the name 'Name' was not found on the selected data source.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: A field or property with the name 'Name' was not found on the selected data source.
0
 
SriVaddadiCommented:
Try this

        Dim dir As New DirectoryInfo(Server.MapPath("~"))
        Dim isoFiles() As FileInfo
        Dim zipFiles() As FileInfo
        Dim exeFiles() As FileInfo
        Dim aspxFiles() As FileInfo
        Dim allFiles(0) As FileInfo

        isoFiles = dir.GetFiles("*.iso")
        zipFiles = dir.GetFiles("*.zip")
        exeFiles = dir.GetFiles("*.exe")
        aspxFiles = dir.GetFiles("*.aspx")
        ReDim allFiles(isoFiles.Length + zipFiles.Length + exeFiles.Length + aspxFiles.Length)
        isoFiles.CopyTo(allFiles, 0)
        zipFiles.CopyTo(allFiles, isoFiles.Length)
        exeFiles.CopyTo(allFiles, isoFiles.Length + zipFiles.Length)
        aspxFiles.CopyTo(allFiles, isoFiles.Length + zipFiles.Length + exeFiles.Length)

        If (allFiles.Length > 1) Then
            articleList.DataSource = allFiles
            articleList.DataBind()
        End If
0
 
ngranickAuthor Commented:
Perfection!! Thanks again Sri!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.