2009-07-16

How to programmatically launch an application under a specific user


ProcessStartInfo psi = new ProcessStartInfo("cmd", @"/C c:\SomeApp.exe");
psi.UserName = "UserName";
psi.Domain = "DomaiName";
psi.Password = ReadPassword("password");
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
using (Process p = Process.Start(psi))
{
// if you need to wait the end of the process
p.WaitForExit();
}

public System.Security.SecureString ReadPassword(string password)
{
System.Security.SecureString secPass = new System.Security.SecureString();
for (int i = 0; i < password.Length; i++)
secPass.AppendChar(password[i]);
return secPass;
}

Комментариев нет: