c# console app does not run from Task Scheduler

moondaddy 916 Reputation points
2021-08-17T08:04:01.773+00:00

I have a real simple console app that needs to be executed every hour from Task Scheduler. For now all it does is write a line to a log file. When I double click on the exe it runs fine. When it runs from Task Scheduler it seems to run OK but the exe fails to write to the log. These the console app's code:

static void Main(string[] args)  
{  
    CallReport();  
}  
  
static void CallReport()  
{  
    string path = Environment.CurrentDirectory + @"\ReportLog.txt";  
    string log = "Executed.  ";    
    log = log + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.CurrentCulture);  
    using (StreamWriter sw = File.AppendText(path))  
    {  
        sw.WriteLine(log);        
    }  
}  

and this is the Task Scheduler History:
123817-image.png

and I'm running the task as Administrator.

and here's the xml from exporting the task:

<?xml version="1.0" encoding="UTF-16"?>  
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">  
  <RegistrationInfo>  
    <Date>2021-08-17T02:00:00.3027276</Date>  
    <Author>CAP1\georgep</Author>  
    <URI>\Refresh_CMTA_Reports</URI>  
  </RegistrationInfo>  
  <Triggers>  
    <CalendarTrigger>  
      <Repetition>  
        <Interval>PT1H</Interval>  
        <Duration>P1D</Duration>  
        <StopAtDurationEnd>false</StopAtDurationEnd>  
      </Repetition>  
      <StartBoundary>2021-08-17T02:05:02</StartBoundary>  
      <ExecutionTimeLimit>PT30M</ExecutionTimeLimit>  
      <Enabled>true</Enabled>  
      <ScheduleByDay>  
        <DaysInterval>1</DaysInterval>  
      </ScheduleByDay>  
    </CalendarTrigger>  
  </Triggers>  
  <Principals>  
    <Principal id="Author">  
      <UserId>S-1-5-21-1073578376-3104900976-2074015838-500</UserId>  
      <LogonType>Password</LogonType>  
      <RunLevel>HighestAvailable</RunLevel>  
    </Principal>  
  </Principals>  
  <Settings>  
    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>  
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>  
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>  
    <AllowHardTerminate>true</AllowHardTerminate>  
    <StartWhenAvailable>false</StartWhenAvailable>  
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>  
    <IdleSettings>  
      <StopOnIdleEnd>true</StopOnIdleEnd>  
      <RestartOnIdle>false</RestartOnIdle>  
    </IdleSettings>  
    <AllowStartOnDemand>true</AllowStartOnDemand>  
    <Enabled>true</Enabled>  
    <Hidden>false</Hidden>  
    <RunOnlyIfIdle>false</RunOnlyIfIdle>  
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>  
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>  
    <WakeToRun>false</WakeToRun>  
    <ExecutionTimeLimit>PT1H</ExecutionTimeLimit>  
    <Priority>7</Priority>  
  </Settings>  
  <Actions Context="Author">  
    <Exec>  
      <Command>E:\Sites\CmtaFieldReportQA\ReportLog\Debug\RefreshSSRS.exe</Command>  
    </Exec>  
  </Actions>  
</Task>  

Thank you.

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 49,541 Reputation points
    2021-08-17T09:36:38.997+00:00

    When you do not specify a "Start In" location for a scheduled task the task scheduler will set the current directory to the System32 folder (usually C:\Windows\System32") for the process that it starts. Have you looked there for the expected log file?

    Also, if your scheduled task is running a 32-bit process then the output file will be in the SysWOW64 folder.

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.