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 Files from Folder / Directory & bind to Asp.net Gridview in C#, VB.NET

Apr 20, 2013
Introduction:

Here I will explain how to get files from folder or directory and 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 files from folder and bind to Gridview in asp.net using C#, VB.NET.

To display files from folder in Gridview we need to write the code like as shown below

C# Code


string[] filesPath = Directory.GetFiles(Server.MapPath("~/SampleFiles/"));
List<ListItem> files = new List<ListItem>();
foreach (string path in filesPath)
{
files.Add(new ListItem(Path.GetFileName(path)));
}
gvDetails.DataSource = files;
gvDetails.DataBind();
VB Code


Dim filesPath As String() = Directory.GetFiles(Server.MapPath("~/SampleFiles/"))
Dim files As New List(Of ListItem)()
For Each path__1 As String In filesPath
files.Add(New ListItem(Path.GetFileName(path__1)))
Next
gvDetails.DataSource = files
gvDetails.DataBind()

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 & bind to gridview in c#.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnGetFiles" Text="Get Files From Folder" runat="server" onclick="btnGetFiles_Click" />
<asp:GridView ID="gvDetails" CellPadding="5" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Text" HeaderText="FileName" />
</Columns>
<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.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
Once you add namespaces write the following code in code behind


// insert files in folder
protected void btnGetFiles_Click(object sender, EventArgs e)
{
BindGridview();
}
// Bind Data to Gridview
protected void BindGridview()
{
string[] filesPath = Directory.GetFiles(Server.MapPath("~/SampleFiles/"));
List<ListItem> files = new List<ListItem>();
foreach (string path in filesPath)
{
files.Add(new ListItem(Path.GetFileName(path)));
}
gvDetails.DataSource = files;
gvDetails.DataBind();
}
VB.NET Code


Imports System.Collections.Generic
Imports System.IO
Imports System.Web.UI.WebControls
Partial Class VBCode1
Inherits System.Web.UI.Page
' 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 filesPath As String() = Directory.GetFiles(Server.MapPath("~/SampleFiles/"))
Dim files As New List(Of ListItem)()
For Each path__1 As String In filesPath
files.Add(New ListItem(Path.GetFileName(path__1)))
Next
gvDetails.DataSource = files
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

17 comments :

rinku said...

it's amazing...thank u Mr.suresh

Anonymous said...

hi Suria...

kemnu chhe lya??? :p

Unknown said...

suresh sir please keep attachment of code for every program so tat we can download the code..

Rakesh said...

Nice

uttham said...

hello suresh

please provide c# code for uploading all files in selected folder. means all files should get uploaded to database when a folder is selected.

thanks in advance

kumar said...
This comment has been removed by the author.
Unknown said...

hello suresh

i want a code that we browse and upload a file to gridview in all format

Unknown said...

Hi suresh..I tried...and awesome.. but how to make it can be opened or linked..Thanks..

Anonymous said...

How to insert, update and delete data into gridview
using jQuery, c#?

Anonymous said...

Hi suresh ....
Its realy nice
How to display all folders from my system in asp.net gridview ?.
ex:- D:\ drive all folders should be display on webpage.

Anonymous said...

Hi suresh ur article is very helpful. But one thing in my mind that if user wants to upload files on different folder i.e pdf file should go in pdf folder or image file in image folder.
Reply soon.
Honey chawla

Anonymous said...

Can we Get Files from Folder / Directory & bind from other website location...???
Like this...
string[] filesPath = Directory.GetFiles("http://www.AnyDomainName.com/Images/");

If yes
Please help me....I spent lots of time for this query.

Unknown said...

M*
u*
j*
l*
g*
this output display using gridview
urgent code send to me

Anonymous said...

I am getting error plz help me, I am news here

HTTP Error 403.14 - Forbidden

Unknown said...

hi i need help . how bind local folder names as tree view. now i get list of local folder name and bind to tree view that all bind has like parent list only . but i need fist folder and in that folder list of folder list . parent to child as like. any help me

Anonymous said...

Nice Tutorial.I need one help from you.How to get the files from folder (ie., upload and download) .Not in server folder.The file accessing in difference drives like d,e, or f .

Anonymous said...

How do I make the files previewable/downloadable? Thanks!

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.