Ruan
Ruan

Reputation: 4303

Server.MapPath - Could not find a part of the path in ASP.net

I am uploading a file to my server using Server.MapPath

When I run my code I get the following error

Could not find a part of the path 'C:\inetpub\wwwroot\wss\VirtualDirectories\80\SitePages\uploads\ABI Employee List.xlsx'.

So Yes, I dont have that directory on my server. I only have a directory up to here.

'C:\inetpub\wwwroot\wss\VirtualDirectories\80\

So, I go and create Those directories.

The weird thing is, is that if I create a folder with the name "SitePages" in the above directory, my site doesn't even want to start up? Delete it and it works again. (Image of error below)

I need to create that directory to upload the file to my server, but I can't, since everything breaks. How will i fix this? enter image description here

Upvotes: 0

Views: 12553

Answers (4)

create a directory in root eg. 'Images' and try the following

protected void Page_Load(object sender, EventArgs e)
{
}

protected void Button1_Click(object sender, EventArgs e)
{
    FileUpload1.SaveAs(Server.MapPath("~\\Images\\" + FileUpload1.FileName));
}

Upvotes: 0

Robin Joseph
Robin Joseph

Reputation: 1325

try to create the desired folder at runtime. you can create a directory by

if(!Directory.Exists("YourDirectory"))
{
Directory.CreateDirectory("YourDirectory")
}

Upvotes: 0

Ayyappan Sekaran
Ayyappan Sekaran

Reputation: 1036

You have create one folder name manually in virtual directory and try this code:

    public static string GetPath()
    {
        string Path = string.Empty;
        try
        {
            Path = HttpContext.Current.Server.MapPath("~/FolderName/");
        }
        catch (Exception _e)
        {
        }
        return Path;
    }

Upvotes: 1

Ankush Jain
Ankush Jain

Reputation: 1527

create a directory in root eg. 'Foldername' and try the following

  DirectoryInfo dir = new DirectoryInfo(HttpContext.Server.MapPath("~/Foldername/"));
            if (!dir.Exists)
            {
                dir.Create();
            }
            // this makes sure that directory has been created
            // do other stuff

Upvotes: 2

Related Questions