133

What I must do to make Server.MapPath work?
I have using System.Web;

what else? When I type Server there is no quick result option (intelli-sense) for Server.

Any help?

Vikrant
  • 4,920
  • 17
  • 48
  • 72
a1204773
  • 6,923
  • 20
  • 64
  • 94
  • 1
    You need to provide more information. What kind of project is this? Why are you wanting to run Server.MapPath? Is this a web-forms project and are you writing code in a Page subclass or in an ASPX's render function? – Dai Jun 19 '12 at 17:06
  • C# Winforms.. i want it to check for file availability on the server – a1204773 Jun 19 '12 at 18:11

9 Answers9

312

you can try using this

    System.Web.HttpContext.Current.Server.MapPath(path);

or use HostingEnvironment.MapPath

    System.Web.Hosting.HostingEnvironment.MapPath(path);
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
DotNetUser
  • 6,494
  • 1
  • 25
  • 27
  • 3
    They not working.... Error 1 The type or namespace name 'HttpContext' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) ... Error 2 The type or namespace name 'Hosting' does not exist in the namespace 'System.Web' – a1204773 Jun 19 '12 at 17:56
  • it looks like your are using winforms, you might wanna see this link then http://www.debugging.com/bug/18951 – DotNetUser Jun 19 '12 at 18:32
  • 4
    Me too, __HostingEnvironment.MapPath__ worked and I am using it outside of a controller. – Luke Oct 08 '14 at 13:40
  • 1
    First of all your project needs to reference System.Web.dll and it will work – ihebiheb Jul 12 '16 at 14:38
  • How to get read a file present in this directory "~/AppData/Roaming"? – Murlidhar Fichadia Apr 24 '19 at 18:32
  • 1
    For my case, `HostingEnvironment.MapPath("/MyFolder")` returns c:\inetpub\wwwroot\. Need to add `HostingEnvironment.MapPath("~/MyFolder")` to return the **correct** path. – sky91 Apr 16 '20 at 04:11
19

Your project needs to reference assembly System.Web.dll. Server is an object of type HttpServerUtility. Example:

HttpContext.Current.Server.MapPath(path);
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
Leandro Gomide
  • 998
  • 1
  • 10
  • 31
  • 3
    Cant't find such reference I found only this two: System.Web.ApplicationServices, System.Web.Services.... – a1204773 Jun 19 '12 at 18:01
8

System.Web.HttpContext.Current.Server.MapPath("~/") gives null if we call it from a thread.

So, Try to use

System.Web.Hosting.HostingEnvironment.MapPath("~/")

Just code
  • 13,553
  • 10
  • 51
  • 93
2

Firt add a reference to System.web, if you don't have. Do that in the References folder.

You can then use Hosting.HostingEnvironment.MapPath(path);

Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
jdisla
  • 139
  • 6
1
bool IsExist = System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("/UploadedFiles/"));
if (!IsExist)
    System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/UploadedFiles/"));

StreamWriter textWriter = File.CreateText(Path.Combine(HttpContext.Current.Server.MapPath("/UploadedFiles/") + "FileName.csv"));
var csvWriter = new CsvWriter(textWriter, System.Globalization.CultureInfo.CurrentCulture);
csvWriter.WriteRecords(classVM);
B. Go
  • 1,436
  • 4
  • 15
  • 22
  • 3
    Code-only answers are considered low quality: make sure to provide an explanation what your code does and how it solves the problem. It will help the asker and future readers both if you can add more information in your post. See [Explaining entirely code-based answers](https://meta.stackexchange.com/questions/114762/) – Calos Mar 12 '20 at 00:55
0

Try adding System.Web as a reference to your project.

Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
jabu.hlong
  • 2,194
  • 20
  • 21
0

You need to add reference (System.Web) Reference to System.Web

Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
0

I know this post is a few years old, but what I do is add this line to the top of your class and you will still be able to user Server.MapPath

Dim Server = HttpContext.Current.Server

or u can make a function

Public Function MapPath(sPath as String)
    return HttpContext.Current.Server.MapPath(sPath)
End Function

I am all about making things easier. I have also added it to my Utilities class just in case i run into this again.

Guy Cothal
  • 1,268
  • 1
  • 10
  • 20
0

I faced the same problem and I guess this might help someone. As the Original Poster of this question asked

What I must do to make Server.MapPath work?
I have using System.Web;

The class which we had written should implement System.Web.UI.Page

Say for example, our classname is MyClass

public class MyClass: System.Web.UI.Page
{
// Code runs here 
}