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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With