HTTPContext .Current为空时如何使用Server.MapPath [英] How to use Server.MapPath when HTTPContext .Current is Nothing

查看:81
本文介绍了HTTPContext .Current为空时如何使用Server.MapPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我需要从Web服务器上的目录中删除一些图像文件时,我有一些可以正常工作的代码:

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)

在后一种情况下, HttpContext.Current.Server.MapPath(ImageURL)的值为Nothing.

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应用程序是相对的.在这种情况下,您将需要将其存储在配置中,或者将两者附加在一起,或者在〜/

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 ~/

这篇关于HTTPContext .Current为空时如何使用Server.MapPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆