计划任务抛出错误

时间:2014-12-05 10:54:14

标签: c# sql

我已经开发了来自数据库的捕获数据程序,并通过Email.Program发送程序,在手动运行时没有任何错误执行。当我在任务调度程序上安排时,它会抛出错误

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

任何人都可以告诉我这是什么原因

1 个答案:

答案 0 :(得分:3)

因为您正在指定模板的相对路径。当任务通过任务调度程序运行时,其当前目录设置为启动exe的可执行文件,显然是C:\Windows\system32

Get your executable's location并构建绝对路径:

using System.IO;
using System.Reflection;

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

string templatePath = Path.Combine(appDirectory, "EmailTemplateMini.html");