Take the 2-minute tour ×

I'm trying to get the absolute path of certain files in a C# class. Server.MapPath works great of course for ASPX and their code-behind pages, but that doesn't exist in another class file. I tried HostingEnvironment.MapPath(), but that complains that the relative virtual path isn't allowed. Any thoughts?

System.Web is already imported.

share|improve this question
you also didn't state where this class was. We can't just assume that the class "external" was still in your web project was it? –  CoffeeAddict Jan 27 '12 at 8:00
@CoffeeAddict: you know this question if 2 1/2 years old, right? –  John Saunders Jan 27 '12 at 8:04

7 Answers 7

up vote 140 down vote accepted

The ServerUtility class is available as an instance in your HttpContext. If you're in an environment where you know it'll be executed inside the ASP.Net pipeline, you can use


You'll have to import System.Web though.

share|improve this answer
Perfect. I didn't realize I had the HttpContext.Current. Thanks. –  Chet Jul 27 '09 at 19:46
Even though this post is more than 2 years old, you've helped me tremendously. Thanks. –  iarwain01 Aug 18 '11 at 19:24
What if it is not executed in that pipeline? –  zaitsman May 5 '13 at 8:50

Can't you just add a reference to System.Web and then you can use Server.MapPath ?

share|improve this answer
Not in external classes. –  infamouse Jul 21 '10 at 19:51
Sure you can add the reference to an external class; but obviously you need to use it in the context of a server request so HttpContext is not null. –  Dan Diplo Jul 21 '10 at 20:14

IF the file you are trying to get is the assembly location for a type. But if the files are relative to the assembly location then you can use this with System.IO namespace to get the exact path of the file.

share|improve this answer
-1: What led you to believe he wanted the location of an assembly? –  John Saunders Jul 27 '09 at 19:49
He said "certain files" he didn't specify the location or nature of the files, thus knowing the assembly location and being able to work relative to that path can be helpful. Of course if he actually stated he was still in a HttpContext then I wouldn't have bothered answering. –  David McEwing Jul 27 '09 at 20:21
David McEwing, what you suggest won't work because IIS doesn't load the assemblies from the location that you install them in, in the web site. They are copied and loaded from a temporary asp.net cache location, so doing a GetAssembly or GetExecutingAssembly will point you to the location of the assembly, but it would have nothing to do with the location of the web site that MapPath points to. –  zumalifeguard May 8 at 2:15
class test
public static void useServerPath(string path)
   if (File.Exists(path)
 \\...... do whatever you wabt

Now when you call the method from the codebehind

for example :

protected void BtAtualizacao_Click(object sender, EventArgs e)
             string path = Server.MapPath("Folder") + "\\anifile.txt";


in this way your code is to simple and with one method u can use multiple path for each call :)

share|improve this answer

I use this too:

share|improve this answer

you can also use:

var path = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/myfile.txt")


var path = Server.MapPath("~/App_Data");
var fullpath = Path.Combine(path , "myfile.txt");

is inaccessible

share|improve this answer

This one helped for me

//System.Web.HttpContext.Current.Server.MapPath //        
FileStream fileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/File.txt"),
FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
share|improve this answer
Welcome to stackoverflow. Little more explanation would help out the fellow programmers to understand why the solution worked out. –  Daenarys 2 days ago

Your Answer


