Run External Application as Another User in C#

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)
            p.StartInfo.Password = encPassword;

            // The UseShellExecute flag must be turned off in order to supply a password:
            p.StartInfo.UseShellExecute = false;


About Jeff Fitzsimons

Jeff Fitzsimons is a software engineer in the California Bay Area. Technical specialties include C++, Win32, and multithreading. Personal interests include rock climbing, cycling, motorcycles, and photography.
This entry was posted in .Net, C#, Technology. Bookmark the permalink.

One Response to Run External Application as Another User in C#

Leave a Reply

Your email address will not be published. Required fields are marked *