Home > Net >  How to get executables directory when executable is called from Windows Task Scheduler
How to get executables directory when executable is called from Windows Task Scheduler

Time:12-02

I have a console application built into an .exe, and a WriteLog module built into a .dll. The WriteLog module is referenced within the console app project. The module is using Directory.GetCurrentDirectory() for creating a Log folder and writing .log files in the same folder next to the executable. This all works as expected when debugging on my dev machine, and also when the .exe is manually executed on the server. However, when the executable is called by Windows Task Scheduler the logs are written to the C:\Windows\SysWOW64 folder. It seems that my Directory.GetCurrentDirectory() is returning the path of the parent process (taskeng.exe or taskmgr.exe) and not the path of the running executable. I have tried a couple of different ways of getting the path like Environment.CurrentDirectory() with no success. How can I get my program to create the log files in the desired location in the same folder as the executable?

Notes: Both the console app and the WriteLog module are written in VB.Net 4.7.2 and the Server is Windows Server 2012 R2 Standard

CodePudding user response:

Got it working. I'm not real familiar with Task Scheduler. More a programmer than a SysAdmin. Here's my fix: In Task Scheduler, open the properties of the task, go to the Actions tab and edit the Start a program Action and set the optional "Start in" value to the path to the folder containing the executable. My logs are now written where I want them. Right next to the executable. (still using Directory.GetCurrentDirectory() in sourcecode)

  • Related