Recursive Directory Listings for ASP.NET in C#

A good, recursive directory lister can come in handy. Especially since IIS’s built-in directory lister doesn’t always cut the mustard. Or, maybe you require this functionality as part of a larger project.

The issue of finding a flexible way to do directory listing, and with recursion, led me to writing my own. Take a look at the (commented!) code below. Feel free to comment with how you’ve implemented this, or even if you have any improvements.

The code below is designed for accepting parameters, in case you have different root directories you’d like to call and list separately. To remove this functionality, simply remove any use of DirParam (and that first if statement).

Here’s my C# code:

public void Main()
{
    string DirParam = Request.QueryString["d"]; //Check for: http://localhost/list.aspx?d=[variable]
    if (DirParam != "tools" && DirParam != "drivers"//Check if someone tried something other than 'tools' or 'drivers', in my particular installation
    {
        Response.Write("No valid directory selected!"); //Can only use 'tools' or 'drivers'
        return//Quit
    }
    DirectoryInfo rootDir = new DirectoryInfo(Server.MapPath("." + "/" + DirParam)); //Set root directory to be server root with selected directory from address bar
    WalkDirTree(rootDir); //Begin walking the tree
}
public void WalkDirTree(DirectoryInfo workingDir)
{
    long FileSizeCheck = 0, FileSize = 0;
    string FileSizeUnit = null;
    FileInfo[] files = workingDir.GetFiles("*.*"); //Files found in directory collection
    DirectoryInfo[] subDir = null//Sub-directories found in directory collection
    Response.Write("<li><b>" + workingDir.Name + "</b></li><ul>" + "\n"); //Write first line in bulleted list (root dir name)
    if (files != null//Keep going until we run out of files
    {
        foreach (FileInfo item in files) //For each individual file in collection...
        {
            FileSizeCheck = item.Length; //Store size of file in memory so we don't access file more than once
            if (FileSizeCheck > 1073741824)
            {
                FileSize = FileSizeCheck / 1024 / 1024 / 1024; //File is larger than 1.00GB, simplify to GB units
                FileSizeUnit = "GB";
            }
            else if (FileSizeCheck > 1048576)
            {
                FileSize = FileSizeCheck / 1024 / 1024; //File is larger than 1.00MB, simplify to MB units
                FileSizeUnit = "MB";
            }
            else if (FileSizeCheck > 1024)
            {
                FileSize = FileSizeCheck / 1024; //File is larger than 1.00KB, simplify to KB units
                FileSizeUnit = "KB";
            }
            else
            {
                FileSize = FileSizeCheck; //File is smaller than 1.00KB, show size as-is
                FileSizeUnit = "B";
            }
            Response.Write("<li><a href=\"" + MapURL(item) + "\">" + item.Name + "</a> [" + FileSize + FileSizeUnit + "]</li>" + "\n"); //...list and create hyperlinks
        }
        subDir = workingDir.GetDirectories(); //Collect sub-directories in current directory
        foreach (DirectoryInfo dir in subDir) //For each directory found in current directory...
            WalkDirTree(dir); //...recursively walk the tree
    }
    Response.Write("</ul>"); //End this sub-directory's list
}
public string MapURL(FileInfo item)
{
string ServerRootPath = Server.MapPath("."); //Determine server root
string url = item.FullName.Substring(ServerRootPath.Length,item.FullName.Length-ServerRootPath.Length); //Subract entire server-side path from server root to determine local URL
return url.Replace("\\""/"); //Transform Windows-style path to HTTP-style by replacing \ with /
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *