Thursday, 20 December 2012

ASP.NET How to get all file names in a folder using C#/VB.NET

ASP.NET How to get all file names in a folder using C#/VB.NET
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);


    }
}


In 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


 output

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.