Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Process with administrator right in C#

I have to start a command line program with System.Diagnostics.Process.Start() and run it as Administrator.

This action will also be run by a Scheduled Task every day.

like image 395
DomBer Avatar asked Nov 05 '10 14:11

DomBer


2 Answers

I've just try to use :

Process p = new Process();
p.StartInfo.Verb = "runas";

this works fine if I'm running my program as Administrator, but when the Scheduled Task runs it, it doesn't take the 'runas' in consideration I think.

like image 73
DomBer Avatar answered Oct 04 '22 02:10

DomBer


A better secure option to run a process with login and password is use the SecureString class to encrypt the password. Here a sample of code:

string pass = "yourpass";
string name ="login";
SecureString str;
ProcessStartInfo startInfo = new ProcessStartInfo();
char[] chArray = pass.ToCharArray();
fixed (char* chRef = chArray)
{
    str = new SecureString(chRef, chArray.Length);
}
startInfo.Password = str;
startInfo.UserName = name;
Process.Start(startInfo);

You must allow unsafe code in your project properties.

Hope this help.

like image 24
Manuel Avatar answered Oct 04 '22 03:10

Manuel