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
Comments
Post a Comment