Filling a Grid with Files in a Folder – C#

This code sample shows, in C#, how to get all the files within a specific folder, and list them all, including the file size, in a Gridview control.

To access the FileSystem, you’ll need to import the System.IO namespace, and to use a DataTable, you must import the System.Data namespace:
using System.IO;
using System.Data;


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_01_Filling_a_Grid_with_Files_in_a_Folder._Default" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
  <div style="text-align:center">
	        <asp:Label ID="lblHeader" runat="server"></asp:Label>
	        <asp:GridView ID="grdFiles" runat="server">
	            </asp:GridView>
	    </div>

    </form>
</body>
</html>

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace _01_Filling_a_Grid_with_Files_in_a_Folder
{
    public partial class _Default : System.Web.UI.Page
    {
        String FilePath;

        protected void Page_Load(object sender, EventArgs e)
        {
            FilePath = Server.MapPath("~/Files");
            GetFiles();  //call the GetFiles method
            lblHeader.Text = "Files in " + FilePath;

        }
        private void GetFiles()
        {
            //Create the DataTable, with columns in which to add the file list
            DataTable dt = new DataTable();
            dt.Columns.Add("FileName", typeof(System.String));
            dt.Columns.Add("Size", typeof(System.String));
            DataRow dr = null;
            DirectoryInfo dir = new DirectoryInfo(FilePath);

            // Iterate through the datatable,
            // adding file to a new row, along with the  filesize to each row
            foreach (FileInfo fi in dir.GetFiles())
            {
                dr = dt.NewRow();
                dr[0] = fi.Name.ToString();
                dr[1] = fi.Length.ToString("N0");  //'N0'formats the number with commas
                dt.Rows.Add(dr);
            }
            // Bind DataTable to GridView - voila!
            grdFiles.DataSource = dt;
            grdFiles.DataBind();
        }

    }
}

 

Related Posts

  • Conditional Gridview Text – Checkboxes
  • Gridview – Conditional Images
  • How to Loop through GridView Rows? GridViewRow
  • How to get OUT parameter from MySQL stored procedure in asp.net
  • How To Make Directory In ASP.NET?
I am Muhammod Arifur Rahman, CEO and founder of Learneveryday.net I worked on asp.net mainly. My second skill language is php and wordpress. I am also work on jquery. I love to work with new problem and feel happy when solve. www.smartdatasoft.com is my own company.

One Comment on "Filling a Grid with Files in a Folder – C#"

  1. Leaidan says:

    That’s a smart way of thnkiing about it.

Got something to say? Go for it!

*

 
More in codesample (2 of 4 articles)