0

I have Developed program for capture data from database and send it via Email.Program executing without any errors while run by manually.when i schedule that on task scheduler its throws an error

Could not find file 'C:\Windows\system32\EmailTemplateMini.html'.
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) at System.Net.WebClient.DownloadString(Uri address) at System.Net.WebClient.DownloadString(String address) at DataEditMailSvc.MailSvc.GenerateEmployeeStatusListMail(DateTime executeDateTime, Int32 isActive, String status, String headerStatus, String connectionString, String locationName) in d:\Work\Projects\Logiwiz\DataEditMailSvc\DataEditMailSvc\MailSvc.cs:line 123

Can any one tell me what is the Reason for this

CodeMind
  • 616
  • 1
  • 7
  • 19
  • Does that file exist? If not that is why the error. If it shouldn't even be looking there then you're going to have to tell us why you think that, possibly with some code examples... ie tell us where you think it should be looking and the code that tells us why it should be looking there. – Chris Dec 05 '14 at 10:58

1 Answers1

3

Because you're specifying a relative path to your template. When a task is run through the task scheduler, its current directory is set to the executable starting your exe, which apparently is C:\Windows\system32.

Get your executable's location and build an absolute path:

using System.IO;
using System.Reflection;

string appDirectory =  Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

string templatePath = Path.Combine(appDirectory, "EmailTemplateMini.html");
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272