This article will show you how you can get the list of all the
files present in the directory which is present in the server in asp.net using
server and bind it to gridview. This article
will cover get the list of all files from server directory in asp.net, asp.net display
list of files from server folder in asp.net using c#.
Some of my previous articles are as follows: How
to Change DateTime Format Using C# In Asp.Net, Facebook
like Button Integration In Asp.net Using C#.Net, Hide
Or Show Gridview Column By Column Index at RunTime In Asp.Net Using C#.Net,
Hide
Or Show Gridview Row By Column Name at RunTime In Asp.Net Using C#.Net, Detect
Header Control and Assign Value of Gridview RowDataBound In Asp.Net Using
C#.Net, Merge
Header or Apply ColumnSpan to Header of GridView In Asp.Net Using C#.Net, Find
Selected Row Value Of GridView on Button Click In Asp.Net Using C#.Net.
So for this article first I will create a new asp.net application. Now add some control in you form.
Here is the page code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Read
List Of File Server and Display In GridView Using C# In Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Click To Get File List" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
</body>
</html>
|
Now add the below code on button click.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
}
protected
void Button1_Click(object
sender, EventArgs e)
{
List<string> fileCollection = new List<string>();
DirectoryInfo
FileDirectory = new DirectoryInfo(Server.MapPath("~/ContentFolder/"));
var
fileList = FileDirectory.GetFiles("*.*",
SearchOption.AllDirectories);
foreach
(var FileName in
fileList)
{
fileCollection.Add(FileName.Name.ToString());
}
GridView1.DataSource =
fileCollection;
GridView1.DataBind();
}
}
}
|
In above code I have first read the collection of all the files and then get the files name. So after this run he application hit the break point.
Here is the final output.
0 comments:
Please let me know your view