Friday, June 3, 2011

GetCurrentDirectory() and Task Scheduler

I recently wrote an account expiration script which notifies users 10 days prior to their passwords expiring. Since the script monitors accounts in different OU's, different messages would be sent out based on a text file located in the executable directory. To ensure the path was correct, I issued the following:
Directory.GetCurrentDirectory() + "\\" + mailFile;

Running the script manually would produce the desired result by loading the file and moving along.  Running the same script using the Task Scheduler would try to load the file from C:\Windows\system32\.

Easy fix:  Ensure you have the path where the executable resides in the “Start in (optional)” text box within the Task Scheduler.

An alternative solution is to use reflection as well as several other methods, but modifying the task was easier than modifying code…

No comments:

Post a Comment