Introduction:
Here
I will explain how to get all files from folder and subfolders in C#
and display or bind to Gridview
in asp.net using C#,
VB.NET or C# Get All Files from Folder and Subfolders and Display it in Gridview in asp.net.
Description:
In
previous articles I explained Download files from gridview with link button in asp.net, Upload images to folder and display it in gridview in asp.net,
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 folder and subfolders 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. |
|||
|
|||
6 comments :
fantastic my upload directory display.Thanks
Hello....
Actually I have a list of items on my html page and i want to bind it with database so that, if i make changes into database items it should reflect immediately to my list items on html page....
I have used DataList yet but it is very heavy control and killing my website performance unnecessarily ....
So, please give me some good ideas.
Thank in Advance
the code is running in a webform, but when i used it with master page the button event don't fire ?
Using VS2012
Hello sir
I am a big fan of your blog.
Anyways, i want to make it dynamic by choosing different folders , Can it possible ?
If yes, Please tell me how?
Thanks in advance
Nice Article. Its really helpful for us.
Sir, I have a treeview, i want to display slected nodes from treeview to datagridview. Can you please tell me how to do it in treeview?
Note: Only a member of this blog may post a comment.