Get File Name of all Files inside Folder (Directory) using C# and VB.Net in ASP.Net

Last Reply 7 months ago By pandeyism

Posted 7 months ago

Hi friends,

How to Get all File names from folder in c# without path ?

example: 

i got only this C:/FOLDER1/img.png

but i need only img.png 

how to get without using any split in c#?

for(int i=0;i<_dataTable.Rows.Count;i++)
{
    string[] filenames = Directory.GetFiles(@"C:\2019\5\");
}

 

Posted 7 months ago

Hi sureshMGR,

Refer below sample.

HTML

<asp:Button Text="GetFileName" runat="server" OnClick="GetNames" />

Namespaces

C#

using System.IO;

VB.Net

Imports System.IO

Code

C#

protected void GetNames(object sender, EventArgs e)
{
    string fileMessage = "";
    DirectoryInfo dir = new DirectoryInfo(@"D:\Users\Ajeet\Images\New folder");
    FileInfo[] fileinfo = dir.GetFiles(@"*.jpg");
    foreach (FileInfo file in fileinfo)
    {
        fileMessage = fileMessage + "\\n" + file.Name;
    }
    ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('" + fileMessage + "');", true);
}

VB.Net

Protected Sub GetNames(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim fileMessage As String = ""
    Dim dir As DirectoryInfo = New DirectoryInfo("D:\Users\Ajeet\Images\New folder")
    Dim fileinfo As FileInfo() = dir.GetFiles("*.jpg")
    For Each file As FileInfo In fileinfo
        fileMessage = fileMessage & "\n" & file.Name
    Next
    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "Message", "alert('" & fileMessage & "');", True)
End Sub

Screenshot