An arbitrary external application can be executed from C# using System.Diagnostics.Process. If you want to run as another user, setting the System.Diagnostics.Process.StartInfo.Password field can be a bit confusing. Here is one way using System.Security.SecureString.AppendChar to avoid having to resort to unsafe code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSRunAs
{
class Program
{
static void Main(string[] args)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
// Domain and User Name:
p.StartInfo.Domain = "optional_domain";
p.StartInfo.UserName = "user_to_run_as";
// Command to execute and arguments:
p.StartInfo.FileName = "c:\\path\\to\\executable.exe";
p.StartInfo.Arguments = "your argument string";
// Build the SecureString password...
System.String rawPassword = "your_password";
System.Security.SecureString encPassword = new System.Security.SecureString();
foreach (System.Char c in rawPassword)
{
encPassword.AppendChar(c);
}
p.StartInfo.Password = encPassword;
// The UseShellExecute flag must be turned off in order to supply a password:
p.StartInfo.UseShellExecute = false;
p.Start();
}
}
}
Thanks man, this helped me out of a jam!….I added commandline options for everything and it works perfect on x64 machine