views:

3394

answers:

3

I have an application which gets called by a scheduled task. It moved from Windows Server 2003 to Windows Server 2008. On 2003, the app ran in the directory where the executable was located. On 2008 Environment.CurrentDirectory (C#) reports that it's running in C:\Windows\System32. How do I set the running directory? I'm using schtasks.exe for command-line deployment.

UPD: Through the interface, it seems to be the "Start in (optional)" field on the action edit screen.

UPD: Looks like using the XML file may help, but I'm looking to do without it.

A: 

Please see my answer to a similar question, of how to set the "Wake the computer to run this task..." option that is only available from the Task Scheduler UI (and via the XML), and not the schtasks.exe /create command line .

The nuts and bolts of it are:

  1. Create your task via schtasks.exe /create /tn MyTask ...
  2. Export your task to XML via schtasks.exe /query /xml /tn MyTask > MyTask.xml
  3. Update this XML via XSLT or a search/replace
  4. Re-import (overwriting the old task) via schtasks.exe /create /tn MyTask /xml MyTask.xml /f

Details are here.

NicJ
Yeah, that's the approach I was talking about in my second update. I guess "can't be done" is a valid answer, so I'll accept.
Eugene Katz
According to the answer <http://stackoverflow.com/questions/447774/specifying-the-running-directory-for-scheduled-tasks-using-schtasks-exe/562149#562149> you are both wrong.
bzlm
+4  A: 

you can set the start in directory using the following command

The key is the \ in the /tr switch.

SCHTASKS /Create /u username /p pswd /ru "NT AUTHORITY\SYSTEM" /rp /sc ONSTART /tn task-name /tr "\"D:\name-of-file-to-run\"

+1  A: 

I recently came across the same issue. The way I resolved it was to add the /V1 switch to the schtasks command.

/V1 creates a pre-vista compatible scheduled task and automatically populates the Start In directory.

works like a charm, as long as your working dir is not just the drive letter (c:\test.bat was setting working dir to c: which is ignored)
Eugene Katz