Scott
Scott

Reputation:

C# Running a winform program as someone other than the logged on user

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

Upvotes: 0

Views: 1308

Answers (5)

JaredPar
JaredPar

Reputation: 754545

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);

Upvotes: 0

Skabumba
Skabumba

Reputation:

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.

Upvotes: 0

Bertvan
Bertvan

Reputation: 5033

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.

Upvotes: 0

ChrisF
ChrisF

Reputation: 137108

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

Upvotes: 1

Ed Guiness
Ed Guiness

Reputation: 35237

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.

Upvotes: 5

Related Questions