2013-05-03 36 views
0
找不到路径的一部分

我使用使用Server.Mappath使用Server.Mappath - 在ASP.net

上传文件到我的服务器当我运行我的代码,我得到以下错误

找不到部分路径 'C:\ inetpub \ wwwroot \ wss \ VirtualDirectories \ 80 \ SitePages \ uploads \ ABI Employee List.xlsx'。

所以是的,我没有在我的服务器上的目录。我只有一个目录到这里。

“C:\的Inetpub \ wwwroot的\ WSS \ VirtualDirectories \ 80 \

所以,我去创造这些目录。

奇怪的是,如果我在上面的目录中创建名称为“SitePages”的文件夹,我的网站甚至不想启动?删除它,它再次运作。 (图片的错误如下)

我需要创建该目录上传文件到我的服务器,但我不能,因为一切都打破。我将如何解决这个问题? enter image description here

回答

2

在root中创建一个目录例如。 “文件夹名称”和尝试以下

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

工作,谢谢。 (我只有SErver.MapPath,而不是HttpContent.Server.MapPath) – Ruan 2013-05-03 12:07:18

1

你有虚拟目录手动创建一个文件夹名称,试试这个代码:

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

尝试在运行时创建所需的文件夹。 你可以通过创建一个目录

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

在root中创建一个目录例如: 'Images'并尝试以下内容

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

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