← All articles
development

ASP.NET File Name at Runtime

A quick ASP.NET snippet and base Page class for grabbing the current page's file name at runtime without repeating the same helper everywhere.

Donn Felker

Donn Felker

2007.01.10 · 1 min read · updated July 5, 2013

Retrieving the ASP.NET filename can be done in many ways. I’ve created this method in a few helper classes in some of the different jobs I have.

public string GetCurrentPageFileName()
{
   return new System.IO.FileInfo(Request.PhysicalPath).Name;
}

I’ve also created a base page class in the past to hold these types of methods (below).

///


/// Base Page class
///

public abstract class BasePage : System.Web.UI.Page
{
    protected string GetCurrentPageFileName()
    {
        return new System.IO.FileInfo(Request.PhysicalPath).Name;
    }
}

Throw this into the App_Code folder and change your default page implementation from System.Web.UI.Page to BasePage. You’ll have all the same methods you did before, but this time with the extra methods. Add more functionality into the BasePage as necessary.

Donn Felker

Written by Donn Felker

I've spent 25+ years shipping software, writing books, and running my own companies. I write about thinking clearly, working for yourself, and doing real work while AI rewrites the rules. New essays land here every week.

Get the newsletter

Keep reading