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

Comments

  1. 15 Oct 2003 at 10:25

    Thats exactly what I needed. Nice one

Leave a comment

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

Related discussion

Related jobs

Events coming up

  • Oct 14

    What’s New in Visual Studio 2008 Service Pack 1?

    Birmingham, United Kingdom

    “Service Pack? We’re calling it a Service Pack? Are you kidding??!?!” Visual Studio 2008 Service Pack 1 will release later in 2008 alongside .NET Framework V3.5 Service Pack 1 and, together, they represent a significant upgrade to Visual Studio 2008. There are enhancements across many areas of the .NET Framework such as data access, windows application development and web development and there are also corresponding changes in the development environment to support the new framework features.