Library code snippets

How to get an array of all files in a directory

This simple code will give you an array containing all files in a directory.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
    void Page_Load(object sender, EventArgs e) {
        FileInfo fi = new FileInfo(Server.MapPath(""));
        DirectoryInfo di = fi.Directory;
        FileSystemInfo[] fsi = di.GetFiles();
        Response.Write("The directory contains the following files and directories:" + di.FullName + "<hr>");
        foreach (FileSystemInfo info in fsi)
            Response.Write(info.Name + "<br>");
    }
</script>

Comments

  1. 15 Oct 2003 at 10:25
    Thats exactly what I needed. Nice one
  2. 01 Jan 1999 at 00:00

    This thread is for discussions of How to get an array of all files in a directory.

Leave a comment

Sign in or Join us (it's free).

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

Related podcasts

Events coming up

  • Mar 15

    DevWeek 2010

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.

Want to stay in touch with what's going on? Follow us on twitter!