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();
}
|
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();
}
|
Demo
![]() |
No comments:
Post a Comment