2013-08-07 101 views
1

我有以下代码:返回使用Server.Mappath的路径与不夹存在

var dir = @"Content\Posts\" + yr + @"\" + mnth + @"\"; 
var a = Path.Combine(dir, dy.ToString() + pId.ToString() + ".txt"); 
//a contains: "Content\\Posts\\2013\\8\\file01.txt" 
stts = obj.NotifyMail(title, writeup, "[email protected]", a); 

,比NotifyMail功能我有这样的:

public bool NotifyMail(string subject, string body, string toAdd, string filePath) 
    { 
     … 
string attachments = HttpContext.Current.Server.MapPath(filePath); 
//NOW here attachments contains: "G:\\Program Files\\Derby\\Work\\Development\\proj\\proj\\`Post`\\Content\\Posts\\2013\\8\\file01.txt" 

      var attchmnts = new LinkedResource(attachments); 
      attchmnts.ContentId = "attchmnts"; 
… 
    } 

现在的问题是在NotifyMailattachments通过Server.MapPath检索物理文件路径返回其路径与包含在里面,即Post一个无效的文件夹,这个文件夹不存在任何地方,甚至没有在硬盘驱动器,我不知道它是如何被拾起返回。但表示,这是由于这个问题LinkedResource(attachments);抛出一个异常:

{"Could not find a part of the path ‘G:\\Program Files\\Derby\\Work\\Development\\proj\\proj\\Post\\Content\\Posts\\2013\\8\\file01.txt"’ 

回答

1

我不相信在MapPath保证存在路径,它只是的策略来上下文路径的虚拟路径。

我觉得你的问题是,你正在使用使用

HttpContext.Current.Request.MapPath 
+0

尝试过,但仍然在返回的路径中额外的未知文件夹。 – Maven

+0

任何在提到“发布”的web.config或IIS应用程序设置?我的猜测是这是一个错误的帖子也许..?此外,可能没有什么区别,但@“内容\文章\” +年+ @“\” + mnth + @“\”应该使用/,而不是\,因为它是一个相对URL,而不是文件路径。 –

0

我有一个类似的问题

HttpContext.Current.Server.MapPath 

尝试,你只需要前添加一个额外的“\\”双反斜线你的文件路径,就像下面和额外的“Post”字(这是你的类名)将会消失。

public bool NotifyMail(string subject, string body, string toAdd, string filePath) 
    { 
string attachments = HttpContext.Current.Server.MapPath(@"\\" + filePath); 

      var attchmnts = new LinkedResource(attachments); 
      attchmnts.ContentId = "attchmnts"; 
    } 
相关问题