Wednesday, September 17, 2008

Displaying the Files in a Directory using a DataGrid

In his article from
Scott Mitchell describe the method.
Here is the c# version

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 WebApplication2
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
var rootFolder = @"C:\Documents and Settings\All Users\Documents\Mes images\Échantillons d'images";

var selectedImages = from file in Directory.GetFiles(rootFolder,"*.jpg")
select new {
Name= new FileInfo(file).Name,


imageList.DataSource = selectedImages;


Here is the aspx page

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">

<head runat="server">




    <form id="form1" runat="server">


        <asp:DataGrid runat="server" ID="imageList" Font-Name="Verdana" 








               <asp:HyperLinkColumn DataNavigateUrlField="Name" 

                                    DataTextField="Name" HeaderText="Nom du Fichier"  


                                    ItemStyle-BackColor="#FF0066" />