Last post Jul 18, 2017 07:46 AM by email@example.com
Jul 18, 2017 01:44 AMfirstname.lastname@example.org|LINK
In the asp.net core .
I want to get the absolute path where a file is located.
No 'MapPath()' method.
Is there any other way to get it's path?
I need some advice.
Jul 18, 2017 02:13 AM|AngelinaJolie|LINK
As far as I know ,the easy way to get the absolute path is the method as shown below which in .net core;
var absolutepath=Directory.GetCurrentDirectory();//to get current absolute path
Jul 18, 2017 02:22 AMemail@example.com|LINK
Hello Angelina Jolie
This method does not have any overloads.
I've tried, and it gets the absolute path of the current application.
What I need is the absolute path of a particular file.
Jul 18, 2017 03:26 AMfirstname.lastname@example.org|LINK
hello Angelina Jolie
I see what you mean. By this method gets the absolute path of the application ,then is spliced together by 'Path.Combine' method.
This is a solution.
Is there a way to get the path directly? Similar to this way，MapPath() ?
Jul 18, 2017 07:28 AM|AngelinaJolie|LINK
I guess that this second way is what you want :
public class HomeController : Controller
private IHostingEnvironment _env;
public HomeController(IHostingEnvironment env)
_env = env;
public IActionResult Index()
var webRoot = _env.WebRootPath;
var file = System.IO.Path.Combine(webRoot, "test.txt");//get the full absolute path
System.IO.File.WriteAllText(file, "Hello World!");
public static string DirectorySeparatorChar = Path.DirectorySeparatorChar.ToString();//To check the path is right(for windows ,it contains "\" ,for IOS ,it contains ":" );
public static bool IsAbsolute(string path)
return Path.VolumeSeparatorChar == ":" ? path.IndexOf(Path.VolumeSeparatorChar) > 0 : path.IndexOf('\\') > 0;
Jul 18, 2017 07:46 AMemail@example.com|LINK
Your suggestion is very helpful to me.
Thanks, Angelina Jolie