May 22, 2015 07:59 by
Peter
Today, I am going to tell you about ASP.NET How to get all file names in a folder using C#/VB.NET. Fisrt, create the new ASP.NET project and then write the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class GetAllFileNames : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = string.Empty;
str = str + "<ul>";
DirectoryInfo d = new DirectoryInfo(@"D:\TextFiles");//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.*"); //Getting Text files
foreach (FileInfo file in Files)
{
str = str+"<li>" + file.Name;
}
str = str + "</li></ul>";
Response.Write(str);
}
}
VB:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.IO
Partial Public Class GetAllFileNames
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim str As String = String.Empty
str = str & "<ul>"
Dim d As New DirectoryInfo("D:\TextFiles")
'Assuming Test is your Folder
Dim Files As FileInfo() = d.GetFiles("*.*")
'Getting Text files
For Each file As FileInfo In Files
str = str & "<li>" & file.Name
Next
str = str & "</li></ul>"
Response.Write(str)
End Sub
End Class
And here is the output from the above code:
Free ASP.NET 5 Hosting
Try our Free ASP.NET 5 Hosting today and your account will be setup soon! You can also take advantage of our Windows & ASP.NET Hosting support with Unlimited Domain, Unlimited Bandwidth, Unlimited Disk Space, etc.