当 HTTPContext .Current 为 Nothing 时如何使用 Server.MapPath
当我需要从网络服务器上的目录中删除一些图像文件时,我有一些代码可以正常工作:
Dim ImageURL As String = dsImages.Tables(0).Rows(iImgRow).Item("ImageURL")
Dim physicalName = Server.MapPath(ImageURL)
oUpload.DeleteFileFromServer(physicalName, iAdid, iImgID)
..但是当以设定的时间间隔在单独的线程中运行的维护任务确定像上面这样的文件时,我遇到了问题需要删除:
Dim ImageURL As String = dsImage.Tables(0).Rows(i - 1).Item("ImageURL")
Dim iImgID As Integer = dsImage.Tables(0).Rows(i - 1).Item("ImageId")
Dim physicalName As String = HttpContext.Current.Server.MapPath(ImageURL)
oUpload.DeleteFileFromServer(physicalName, iAdID, iImgID)
在后一种情况下,HttpContext.Current.Server.MapPath(ImageURL) 的值为 Nothing。
有没有办法获得此案例的完整路径?
I have some code that works fine when I need to delete some image files from a directory on my web server:
Dim ImageURL As String = dsImages.Tables(0).Rows(iImgRow).Item("ImageURL")
Dim physicalName = Server.MapPath(ImageURL)
oUpload.DeleteFileFromServer(physicalName, iAdid, iImgID)
..but I am running into a problem when a maintenance task running in a separate thread at set intervals determines that files like the above need to be deleted:
Dim ImageURL As String = dsImage.Tables(0).Rows(i - 1).Item("ImageURL")
Dim iImgID As Integer = dsImage.Tables(0).Rows(i - 1).Item("ImageId")
Dim physicalName As String = HttpContext.Current.Server.MapPath(ImageURL)
oUpload.DeleteFileFromServer(physicalName, iAdID, iImgID)
In this latter case, HttpContext.Current.Server.MapPath(ImageURL) has a value of Nothing.
Is there a way to get the full path for this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您的代码在线程内运行时,
HttpContext.Current
不可用。要获得您的 Web 应用程序路径,您可以使用 :
或者您可以简单地在
HttpRuntime.AppDomainAppPath
属性中找到它(推荐/更快)。The
HttpContext.Current
is not available when your code is running inside a thread.To have your web application path you can either use :
or you can simply find it in the
HttpRuntime.AppDomainAppPath
property (recommended/faster).假设路径是相对的,那么单独的进程不知道它们相对于哪个 Web 应用程序。在这种情况下,您需要将其存储在配置中,并将两者附加在一起或在 ~/ 上执行字符串替换
Assuming that the paths are relative then the separate process does not know what they are relative to, which web application. In this case you will need to store it in the config and either append the two together or perform a string replace on ~/