codiiv codiiv - 12 days ago 7
C# Question

.NET MVC get path of a folder. Any Folder I need

I am new on .NET and created a project XYZ. In one of the controllers, I created QR codes and save them in XYZ/Content/Uploads as long as I hard code the path.

Something like

img.Save("C:\\Users\\myusername\\Documents\\Visual Studio 2015\\Projects\\XYZ\\XYZ\\Content\\Uploads\\"+newname+ ".jpg", ImageFormat.Jpeg);


When I deploy the project on Azure, obviously this doesn't work. I want to get physical path of the Content folder and so far the suggested
Server.Path("~/Content/Uploads")
is simply not working.

Can someone tell me why on earth something so trivial has to seem so hard to come across, and point me in the right direction :)

Answer

Application as root.

var folderRootPath = HttpContext.Server.MapPath("~/Content/Upload/");

or

VirtualPathUtility.ToAbsolute("~/Content/Upload/")

or

 HostingEnvironment.MapPath("~/Content/Upload/");
Comments