Cannot use Server.MapPath
Asked Answered
G

9

135

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?

Guidon answered 19/6, 2012 at 16:59 Comment(2)
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?Cyrilcyrill
C# Winforms.. i want it to check for file availability on the serverGuidon
V
314

you can try using this

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

or use HostingEnvironment.MapPath

    System.Web.Hosting.HostingEnvironment.MapPath(path);
Ventriculus answered 19/6, 2012 at 17:10 Comment(6)
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'Guidon
it looks like your are using winforms, you might wanna see this link then debugging.com/bug/18951Ventriculus
Me too, HostingEnvironment.MapPath worked and I am using it outside of a controller.Outfight
First of all your project needs to reference System.Web.dll and it will workBayard
How to get read a file present in this directory "~/AppData/Roaming"?Nutritious
For my case, HostingEnvironment.MapPath("/MyFolder") returns c:\inetpub\wwwroot\. Need to add HostingEnvironment.MapPath("~/MyFolder") to return the correct path.Melodiemelodion
E
19

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

HttpContext.Current.Server.MapPath(path);
Ekaterinodar answered 19/6, 2012 at 17:6 Comment(1)
Cant't find such reference I found only this two: System.Web.ApplicationServices, System.Web.Services....Guidon
C
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("~/")

Cerell answered 9/7, 2018 at 14:0 Comment(0)
W
3

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

Writer answered 2/2, 2016 at 3:44 Comment(0)
O
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);
Outspeak answered 11/3, 2020 at 23:59 Comment(1)
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 answersObaza
D
0

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

Downpipe answered 28/8, 2015 at 6:24 Comment(0)
S
0

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

Skeptical answered 7/10, 2015 at 9:52 Comment(0)
S
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.

Scraggy answered 6/4, 2020 at 12:41 Comment(0)
T
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 
}
Trinitrophenol answered 6/7, 2021 at 8:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.