Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# Get All Files from Folder and Subfolders & Display it in Gridview in asp.net

Aug 26, 2013
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:


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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

6 comments :

Irtekaz Ahmed Khan said...

fantastic my upload directory display.Thanks

Anonymous said...

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

Unknown said...

the code is running in a webform, but when i used it with master page the button event don't fire ?

Using VS2012

Anonymous said...

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

sanju said...

Nice Article. Its really helpful for us.

Unknown said...

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?

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.