3

I have a console app which is located on my desktop. I have set it to a Scheduled Task to run every 20 Minutes indefinitely. I have turned off auto sleep/hibernate. Then I left my PC ON and locked my desktop for the weekend (2-3 days).

My console app was developed to email me every time it catches an exception. When I returned, checked my inbox received a couple of error emails containing

Access to the path 'C:\WINDOWS\system32\myLogs\' is denied.

it seemed my console app was being run from System32 not from my Desktop.

Q: Why is it behaving like it?


this is my string on creating my myLog folder path

var logpath = Directory.GetCurrentDirectory() + Properties.Settings.Default.LogPath;

this checks if the folder exists, if not it creates a new folder.

if (!Directory.Exists(logpath))
   Directory.CreateDirectory(logpath);

I believe the error was triggered on checking/creating the folder. My app should create the myLog folder in the same directory as my console app.

Q: Why is it running from System32 Folder?

Hexxed
  • 683
  • 1
  • 10
  • 28

2 Answers2

11

Scheduled Tasks are launched by the Task Scheduler service. This service runs inside the C:\Windows\System32\svchost.exe executable. By default, all applications launched by the Task Scheduler are launched with C:\Windows\System32 as the current directory.

You can change the start directory in the Edit Action dialog of the Task Scheduler:

Task Scheduler: Edit Action dialog

You can use environment variables. For example, %USERPROFILE% will set the start directory to the user's profile directory (eg. C:\Users\MyUsername).

Rather than changing the start directory for the scheduled task, you may want to find the the directory where the console application executable is located:

System.Reflection.Assembly.GetExecutingAssembly().Location

Community
  • 1
  • 1
Serge
  • 3,986
  • 2
  • 17
  • 37
  • So its better to find the directory of my console app than to tweak with the Task Scheduler? – Hexxed Feb 13 '17 at 03:14
  • Depends solely on what you want. If the application should create a Log directory _in the directory in which the EXE was started_ (1), then don't make any code changes and tweak the Task Scheduler settings instead. If the application should create a Log directory _in the same directory as the EXE_ (1), then use `System.Reflection.Assembly.GetExecutingAssembly().Location`. You may code for both, eg try directory (1) first, if this fails, then try directory (2). – Serge Feb 13 '17 at 03:33
  • Thanks, I'll use scenario (2). – Hexxed Feb 13 '17 at 03:43
-1

Console applications are "command line" and they are run from CMD.EXE which is usually located in C:\Windows\System32\

I suspect the scheduled task sets the current directory to where CMD.EXE is located and then launches your application by specifying the full folder,

(note there is one in \Syswow64\ for 32-bit command lines for 64-bit machines).

N.E.
  • 1
  • my machine is in 64Bit but I designed my app with a Platform target of x86 (32Bit) due to issues with an old Database im connecting to. – Hexxed Feb 13 '17 at 03:16