4

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

4
  • What's inside path when you try this? Commented Mar 9, 2012 at 11:43
  • You need to have assigned appropriate permissions for the ASP.Net application to be able to read and write to the directory. Secondly look at HttpServerUtility.MapPath to find out about application paths. msdn.microsoft.com/en-us/library/… Commented Mar 9, 2012 at 11:44
  • hmm "~/Content/albums\\gone" gone is the title of the album @SimonEdström Commented Mar 9, 2012 at 11:45
  • 1
    That dosn't sound like a valid path ;-) Commented Mar 9, 2012 at 11:46

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

2

Try this insteed, as you point out it's not a valid path you have.

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

 Directory.CreateDirectory(path);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.