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>

You might also like...

Comments

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.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Before software should be reusable, it should be usable.” - Ralph Johnson