user3163213
user3163213

Reputation: 701

creating nested folder in c# shows access denied error

I am trying to create folders in nested manner.

if (file.ContentLength > 0 && file != null)
{
     string path = "~/Videos/" + Session["username"] + "_" + Session["userid"];
     if (!Directory.Exists(path))
     {
         Directory.CreateDirectory(path);
     }
     string filename = path + file.FileName;
     filepath = "/Videos/" + Session["username"] + "_" + Session["userid"];
     file.SaveAs(filename);

If you see here- /Videos/ folder is what I have currently on disk. Where another folder with user's name and id is what I want to create inside this Videos folder. How Would I be creating this folder inside this folder?

Because currently it is showing me this error -

Access to the path '~/Videos/shaun_2' is denied.

I tried restarting visual studio with administrator's credentials. But it still remains here.

Upvotes: 0

Views: 79

Answers (1)

Stephan Bauer
Stephan Bauer

Reputation: 9249

I'm assuming that you are using ASP.NET: try to use Server.MapPath("~/...") to get the physical path

See MSDN

Upvotes: 1

Related Questions