How To Get A List Of Files From Folder In ASP.NET?
If you need to get a list of files from some folder you need to use DirectoryInfo class from System.IO namespace. One sample approach that shows all gif images from Images folder in ListBox control named lstFiles, could be like this:
[ C# ]
// We
need this namespace
using
System.IO;
public
partial class
_Default : System.Web.UI.Page
{
protected void
Page_Load(object sender,
EventArgs e)
{
// We'll read all files from Images subfolder
DirectoryInfo diFiles =
new DirectoryInfo(Server.MapPath("~/Images/"));
// Takes all .gif images,
// You can set some other filter or
// *.* for all files
lstFiles.DataSource = diFiles.GetFiles("*.gif");
// bind data to list box
lstFiles.DataBind();
}
}
[ VB.NET ]
' We
need this namespace
Imports
System.IO
Partial
Class _Default
Inherits System.Web.UI.Page
Protected Sub
Page_Load(ByVal sender
As Object, ByVal
e As System.EventArgs)
Handles Me.Load
' We'll read all files from Images subfolder
Dim diFiles As
DirectoryInfo = New
DirectoryInfo(Server.MapPath("~/Images/"))
' Takes all .gif images,
' You can set some other filter or
' *.* for all files
lstFiles.DataSource = diFiles.GetFiles("*.gif")
' bind data to list box
lstFiles.DataBind()
End Sub
End
Class
Related articles:
1. Caching Techniques in ASP.NET 2.0