connectseekers.com

Monday, 23 March 2015

Read List Of File From Server and Display In GridView Using C# In Asp.Net

3/23/2015 - By Pranav Singh 0

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#.


So for this article first I will create a new asp.net application. Now add some control in you form.



Now we will create a directory and add some files.



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.



Tags: , ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top