Introduction:
Here
I will explain how to get all files from directory and subdirectories in C#
and display or bind to Gridview in asp.net
using C#, VB.NET.
Description:
In
previous articles I explained create zip files in asp.net, Delete files from uploaded folder in asp.net, create/delete directory in asp.net, Joins in SQL Server and many articles relating to Gridview,
SQL , jQuery , asp.net,
C#,
VB.NET.
Now I will explain how to get all files from directory and subdirectories and bind
to Gridview in asp.net
using C#, VB.NET.
To
display all files from folder and subfolders in Gridview
we need to write the code like as shown below
C#
Code
// Bind Data to Gridview
protected void
BindGridview()
{
string strpath = @"E:\Internet
Tips\";
string[] folders = Directory.GetFiles(strpath,
"*", SearchOption.AllDirectories);
gvDetails.DataSource = folders;
gvDetails.DataBind();
}
|
VB
Code
' Bind Data to Gridview
Protected Sub
BindGridview()
Dim strpath As String = "E:\Internet
Tips\"
Dim folders As String() = Directory.GetFiles(strpath, "*", SearchOption.AllDirectories)
gvDetails.DataSource = folders
gvDetails.DataBind()
End Sub
|
If
you want to check it in complete example write the following code
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Get files from folder & subfolder & display it in
gridview in c#.net</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:Button ID="btnGetFiles"
Text="Get Files
From Folder & Sub Folders" runat="server" onclick="btnGetFiles_Click" />
<asp:GridView ID="gvDetails"
CellPadding="5"
runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
|
Now in code behind add the following namespaces
C#
Code
using System;
using System.IO;
|
Once you add namespaces write the following code in
code behind
// Get files in folder
protected void
btnGetFiles_Click(object sender, EventArgs e)
{
BindGridview();
}
// Bind Data to Gridview
protected void
BindGridview()
{
string strpath = @"E:\Internet
Tips\";
string[] folders = Directory.GetFiles(strpath,
"*", SearchOption.AllDirectories);
gvDetails.DataSource = folders;
gvDetails.DataBind();
}
|
VB.NET
Code
Imports System.IO
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub
Page_Load(ByVal sender As Object, ByVal e As
EventArgs)
End Sub
' insert files in folder
Protected Sub
btnGetFiles_Click(ByVal sender As Object, ByVal e As
EventArgs)
BindGridview()
End Sub
' Bind Data to Gridview
Protected Sub
BindGridview()
Dim strpath As String = "E:\Internet
Tips\"
Dim folders As String() = Directory.GetFiles(strpath, "*", SearchOption.AllDirectories)
gvDetails.DataSource = folders
gvDetails.DataBind()
End Sub
End Class
|
Demo
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
2 comments :
sir,code is working fine for me,but i want to read only .jpg or .png file
Change string[] folders = Directory.GetFiles(strpath, "*", SearchOption.AllDirectories);
To
string[] folders = Directory.GetFiles(strpath, ".jpeg", SearchOption.AllDirectories);
Note: Only a member of this blog may post a comment.