如何使用server.mappath当httpcontext .current无需
-
12-10-2019 - |
题
当我需要从Web服务器上的目录中删除某些映像文件时,我有一些可以正常工作的代码:
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) 一无所有。
有没有办法为这种情况获得完整的道路?
解决方案
假设路径是相对的,那么单独的过程不知道它们相对于哪个Web应用程序。在这种情况下,您需要将其存储在配置中,然后将两者加在一起,或者在〜/上执行字符串替换
其他提示
这 HttpContext.Current
当您的代码在线程中运行时,不可用。
要拥有您的Web应用程序路径,您可以使用:
System.Web.Hosting.HostingEnvironment.MapPath("~/")
或者,您可以简单地在 HttpRuntime.AppDomainAppPath
属性(推荐/更快)。
不隶属于 StackOverflow