1

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.

  • "Not working" is not a proper problem description. Please read [ask] and provide all relevant information and your research. Also, I hope you realize web servers can't write to the client's desktop. It's working during development because your machine acts both as server and client at the time. – CodeCaster Jun 25 '16 at 08:32
  • writing to the desktop is not the problem. The problem is that reading the path `Server.MapPath("../Data")` –  Jun 25 '16 at 15:35
  • I am asking what the problem **is**. What do you expect to happen, what actually happens? – CodeCaster Jun 25 '16 at 15:39
  • I want to write the `Server.MapPath("../Data")` to my file hello1.txt. It's creating the file hello1.txt if not exist, but it's empty no content inside, even "Hello world" is not printed –  Jun 25 '16 at 18:17
  • I understand that. Please tell what this code actually does. We can't give you an answer if you don't explain what is happening. – CodeCaster Jun 25 '16 at 18:18
  • This is part of a project I am working on, I need to schedule an event every day. I wrote a method what should be scheduled and it's executing if I try a simple method call. But when I try to schedule it using Quartz.Net. It did not work. My task contains a lot of `HttpContext` context. I found out this is why my method was not working, so I am trying this simple example –  Jun 25 '16 at 18:28
  • Listen. We cannot answer "did not work" and "was not working". **What exactly is happening**? Any exception, any output, is the file being written somewhere else? – CodeCaster Jun 25 '16 at 18:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115601/discussion-between-saai-and-codecaster). –  Jun 25 '16 at 18:34

2 Answers2

8

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.

Sergey Mirvoda
  • 3,209
  • 2
  • 26
  • 30
Saahithyan Vigneswaran
  • 6,841
  • 3
  • 35
  • 45
-1

you can use System.Web.Hosting.HostingEnvironment.MapPath method.

H. Aghassi
  • 221
  • 4
  • 16