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

Related podcasts

We'd love to hear what you think! Submit ideas or give us feedback