140

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.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
Chet
  • 21,375
  • 10
  • 40
  • 58

9 Answers9

318

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

HttpContext.Current.Server.MapPath()

You'll have to import System.Web though.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
womp
  • 115,835
  • 26
  • 236
  • 269
  • 1
    Even though this post is more than 2 years old, you've helped me tremendously. Thanks. – iarwain01 Aug 18 '11 at 19:24
  • 6
    What if it is not executed in that pipeline? – zaitsman May 05 '13 at 08:50
  • If you're not executing inside of ASP.Net then it's unlikely that your HttpContext is being set, unless you wrote your own pipeline :) You'll have to rely on whatever methods your execution context (router?) provides. If your process has insight to the basics of your path routing you can take a look at the System.IO.Path methods. – womp Apr 05 '16 at 18:40
  • This compiled but context is null outside of controllers, so my code errors. I think womp is saying the same thing there. If thats the case can you access MapPath outside of the routers? – edencorbin Feb 25 '19 at 02:13
34

you can also use:

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

if

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

is inaccessible

Yakir Manor
  • 4,687
  • 1
  • 32
  • 25
10

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

Edit: Nowadays I'd recommend using the HostingEnvironment.MapPath Method:

It's a static method in System.Web assembly that Maps a virtual path to a physical path on the server. It doesn't require a reference to HttpContext.

Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
  • 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
4
System.Reflection.Assembly.GetAssembly(type).Location

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.

David McEwing
  • 3,320
  • 18
  • 16
  • -1: What led you to believe he wanted the location of an assembly? – John Saunders Jul 27 '09 at 19:49
  • 1
    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 08 '14 at 02:15
4

I use this too:

System.Web.HTTPContext.Current.Server.MapPath
soamazing
  • 1,656
  • 9
  • 25
  • 32
3

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);
Nagama Inamdar
  • 2,851
  • 22
  • 39
  • 48
netuser
  • 31
  • 1
  • Welcome to stackoverflow. Little more explanation would help out the fellow programmers to understand why the solution worked out. – Nagama Inamdar Dec 26 '14 at 08:48
3
class test
{
public static void useServerPath(string path)
{
   if (File.Exists(path)
{
 \\...... do whatever you wabt
}
else
{
\\.....
}
}

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";

            test.useServerPath(path);
}

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

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
Ahmad
  • 91
  • 1
  • 1
2

Whether you're running within the context of ASP.NET or not, you should be able to use HostingEnvironment.ApplicationPhysicalPath

daudihus
  • 159
  • 1
  • 8
0

The server.mappath("") will work on aspx page,if you want to get the absolute path from a class file you have to use this-

HttpContext.Current.Server.MapPath("~/EmailLogic/RegistrationTemplate.html")
Debendra Dash
  • 5,334
  • 46
  • 38