0

I have created a console app that I am trying to run in Task Scheduler. When I run it by clicking the executable or using a shortcut it works fine but when I run it in Task Scheduler I get "The app for PHLIP231 Prod had Access to the path 'C:\WINDOWS\DMBErrors' is denied."

I have searched my app for this path and have even tried manually adding the path to my computer with no success. My app builds an excel file and writes it to a path relative to the executable.

This is the code I use for doing this:

string strFileName = "";
string strCurrentDirectory = Path.GetDirectoryName(Environment.CurrentDirectory);
string strDMBErrors = Path.Combine(strCurrentDirectory, "DMBErrors");
DirectoryInfo directory = new DirectoryInfo(strDMBErrors);
if (!Directory.Exists(strDMBErrors))
{
   Directory.CreateDirectory(strDMBErrors);
}
strFileName = strDMBErrors + "\\PHLIP251ErrorsProd" + strDate2 + Sender + ".xls";
excelApp.ActiveWorkbook.SaveAs(strFileName);

Any help would be appreciated. Thanks.

Donald
  • 1
  • 1
  • You may specify current directory of process, check this https://stackoverflow.com/questions/447774/specifying-the-running-directory-for-scheduled-tasks-using-schtasks-exe – buithienquyet Feb 17 '22 at 17:20
  • I added the Start In directory and that fixed the problem. Thank you for the advice. – Donald Feb 18 '22 at 18:10
  • I may have jumped the gun adding my answer. It appeared to work once but then it started giving me: Task Scheduler failed to launch action "C:\Users\nkr7\Desktop\Projects\ConsoleApp\CommandLine\DMBErrors\DMBErrorsProdExcel\bin\Release\netcoreapp3.1\DMBErrorsProdExcel.exe" in instance "{cfb5a55c-011e-41a3-89a8-ccc0a7796352}" of task "\DMBErrors Prod". Additional Data: Error Value: 2147942667. This only happens on my apps that writes Excel sheets. My apps that don't write files work fine in Task Scheduler. – Donald Feb 25 '22 at 14:50
  • Did you check this https://stackoverflow.com/a/14326101/7174186 – buithienquyet Feb 25 '22 at 15:21
  • Yes, there are no quotes in the path. This is copied straight from Task Scheduler: C:\Users\nkr7\Desktop\Projects\Console Apps\DMBErrors\DMBErrorsProdExcel\bin\Release\netcoreapp3.1 – Donald Feb 25 '22 at 17:07
  • I am thoroughly confused now. I went in and commented out where I write the Excel file and ran it through Task Scheduler and it worked fine. I uncommented the lines and reran it through Task Scheduler and it appears to be working fine now. – Donald Feb 28 '22 at 14:29

1 Answers1

0

I fixed it by commenting out the lines that write the Excel sheets, running the app in Task Scheduler, uncommenting the lines and rerunning it in Task Scheduler.

Donald
  • 1
  • 1