Getting current directory in .NET web application

Julian Coltea picture Julian Coltea · Jun 8, 2012 · Viewed 206.2k times · Source

So I have a web project, and I'm trying to get the root directory of the website using the c# method Directory.GetCurrentDirectory(). I don't want to be using a static path as the file locations will be changing in the future. This method is running in my imageProcess.aspx.cs file, but where I thought it would return:

C:\Users\tcbl\documents\visual studio 2010\Projects\ModelMonitoring\ModelMonitoring\imageProcess.aspx.cs

I'm instead getting:

C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\

Can anyone explain why this is happening and what a possible solution might be? Thanks a lot.

Answer

SLaks picture SLaks · Jun 8, 2012

The current directory is a system-level feature; it returns the directory that the server was launched from. It has nothing to do with the website.

You want HttpRuntime.AppDomainAppPath.

If you're in an HTTP request, you can also call Server.MapPath("~/Whatever").