How to access the HttpServerUtility.MapPath method in a Thread or Timer?

前端 未结 6 1915
梦毁少年i
梦毁少年i 2020-12-23 15:46

I use a System.Timers.Timer in my Asp.Net application and I need to use the HttpServerUtility.MapPath method which seems to be only available via <

6条回答
  •  半阙折子戏
    2020-12-23 16:30

    When the timer elapse, there is no current HTTP context. This is because the timer events are not related to a specific HTTP request.

    What you should do is use HttpServerUtility.MapPath where HTTP context is available. You can do it in one of the request pipeline events (such as Page_Load) or in a Global.asax event such as Application_Start.

    Assign the MapPath result to a variable accessible from the Timer.Elapsed event, where you could use Path.Combine to get the location of a specific file you need.

提交回复
热议问题