asp.net提到Local中的路径名

时间:2012-04-30 08:12:44

标签: asp.net

我在类文件中使用Exec msdb.dbo.sp_send_dbmail使用字符串构建器来附加from和To地址。现在我需要附上将在Local solution文件夹中的文件(在我创建文件夹的解决方案中)< / p>

          strSql.Append("Exec msdb.dbo.sp_send_dbmail @profile_name='SLAToolProfile',");strSql.Append("@recipients='");
        strSql.Append(eMailToaddresses);

        strSql.Append("',@file_attachments='");
        string Path = "~/Material/study material.doc";
        strSql.Append(Path);

当我使用上述格式时,我得到了文件附加错误。如何在解决方案中提到应该在本地文件夹中的路径?

2 个答案:

答案 0 :(得分:0)

您需要使用VirtualPathUtility.ToAbsolute方法:

strSql.Append("Exec msdb.dbo.sp_send_dbmail @profile_name='SLAToolProfile',");strSql.Append("@recipients='");
        strSql.Append(eMailToaddresses);

        strSql.Append("',@file_attachments='");
        string Path = VirtualPathUtility.ToAbsolute("~/Material/study material.doc");
        strSql.Append(Path);

应该有效

http://msdn.microsoft.com/en-us/library/ms150160(v=vs.90).aspx

<强>更新

确实做了一点挖掘,结果发现当你使用sp_send_dbmail时,附件位置是相对于数据库服务器的位置,而不是运行代码的机器。因此,您遇到的第一个问题是使数据库服务器可以访问此文件位置,即使用文件共享和UNC位置。所以你不能使用~。 Db服务器无法理解您的路线在哪里......!

答案 1 :(得分:0)

使用此:

string filename = "study material.doc";
string storePath = Server.MapPath("~/Material");
string finalPath = Path.Combine(storePath,filename);

Server.MapPath()将您的虚拟路径转换为绝对应用程序路径;