简体   繁体   中英

Map path in Quartz.net in ASP.NET & C#

I am using Quartz.Net in Asp.Net C# to schedule my task. I want to write to a file, when I try without the MapPath it's writing to the file as expected (Hello world).

Here is the code what I wrote. I have tried HostingEnvironment.MapPath and Server.MapPath . It's simply writing the file without any content.

public void Execute(IJobExecutionContext context)
{
    StreamWriter file = new StreamWriter("destination_actualPath\\hello1.txt", true);
    file.WriteLine("Hello world " + Server.MapPath("../Data"));
    file.Close();
}

Yes this folder Data do exist in my project directory. This function is inside the IJob class. If I write a new method (other than execute) it's writing to the file (hello world and path), it's not writing any content to the file if it's inside this Execute method.

try

file2.WriteLine("Hello World " + System.Web.Hosting.HostingEnvironment.MapPath("~/Data"));

HttpContext is not allowed in Quartz.net, so we have to use HostingEnvironment instead. Here we usually refer virtual path using ~ , not ../ . I guess this is your mistake.

您可以使用System.Web.Hosting.HostingEnvironment.MapPath方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM