Hi I am trying to create a new directory usingC#/asp.net webforms but I do not seem to have any luck.Here is my code:
string path = "~/Images/avatar/" + User.Identity.Name + "/userAvatars/";
private void createPath(string path)
{
string activeDir = @"~/Images/avatar/userAvatars";
if( !Directory.Exists(Server.MapPath(path)) )
{
string newPath = Path.Combine(activeDir, User.Identity.Name);
Directory.CreateDirectory(newPath);
}
}
createPath(path)
Now this code does not throw any errors but it does not create the folder either unless I provide a physical directory starting from the letter of the drive.Example:
D:\Projects IDE\Visual Studio\MyWork\Websites\Forum
This won’t work because at some point I will want to upload the application to the server.
So how can I set the filepath to get the root if the project and make this code work?
Try this
You are forgetting to map the newPath after you use Path.Combine.