Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create relative folder using c#.net

Hello guys i am trying to create a folder using c# mvc3. I have the following code in my controller

string path = Path.Combine("~/Content/albums", album.title.Replace(" ", ""));
Directory.CreateDirectory(path);

but it does not seem to create a folder. I have tried using directory without relative paths and it works

Directory.CreateDirectory("c:/test");

Thank you

like image 523
Charbel Wakim Avatar asked Dec 27 '22 04:12

Charbel Wakim


1 Answers

Try the Server.MapPath (if in the Controller) or System.Web.Hosting.HostingEnvironment.MapPath (if outside the Controller) first on the Content, it maps the virtual resource path to the physical path, so:

string contentPath = Server.MapPath("~/Content/albums");
string path = Path.Combine(contentPath, album.title.Replace(" ", ""));

And then create the directory.

like image 101
Patryk Ćwiek Avatar answered Jan 14 '23 16:01

Patryk Ćwiek