Search code examples
c#windowsexeapplication-settings

Run a Windows App using a specific User account


I need to ensure that my widnows app (winform not console) runs under a certain user account (in other words any user can be logged on to the maching but the .exe will always execute as the specified user).

Can this be done programtically? If so, how?


Solution

  • You can start the application like this:

    ProcessStartInfo psi = new ProcessStartInfo(myPath);
    psi.UserName = username;
    
    SecureString ss = new SecureString();
    foreach (char c in password)
    {
     ss.AppendChar(c);
    }
    
    psi.Password = ss;
    psi.UseShellExecute = false;
    Process.Start(psi);