2013-08-07 173 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"; 
    } 
相關問題