views:

371

answers:

5

I need my winform program to run as another user (it will run under task scheduler) and not the logged on user. I suspect the trouble is my app is gui based and not command line based (does this make a difference) so the gui needs to load do its thing and then close. Is this possibly under XP or Vista?

Thanks

+5  A: 

Scheduled Tasks can be 'run as' a specified user, which can be different to the logged-in user.

You can specify this user when creating the task, or by editing the properties of an existing task.

Ed Guiness
+1  A: 

If your app needs to run as a sheduled task then it can't really have a UI. As a bare minimum it should really be capable of being run via the command line.

The best approach would be to separate the UI from the actual processing so that the you can still run it interactively if required. This would also make testing your app a whole lot easier.

EDIT: Edited for typing and sense

ChrisF
A: 

I'd vote for the first option, or to provide an extra possibility: Use Impersonation in your code. Although it might be overkill/not fit your needs here.

Bertvan
A: 

Another way you could execute the program is to Programmatically change the user based on a config file or even a DB connection. One sample project is on Code Project:

http://www.codeproject.com/KB/cs/runas.aspx

Hope this helps.

A: 

You're looking for the Process.Start method. One of the overloads accepts a user name / password pair. The process will be created using those user credentials.

var app = Process.Start(@"c:\path\to\some\app.exe", userName, password, domainOrEmptyString);
JaredPar