@manhng

Welcome to my blog!

Console Runas Admin

November 26, 2021 10:29

Console Runas Admin (edit)

using System.Diagnostics;
using System.Linq;
using System.Security;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo psi = new ProcessStartInfo(@"C:\Program\Test"); //Test.exe
            psi.Verb = "runas";
            psi.UseShellExecute = false; //To use runas verb, here must be false
            psi.CreateNoWindow = true; //Hides Console
            psi.WindowStyle = ProcessWindowStyle.Hidden; //Hides GUI
            psi.UserName = "userName";
            SecureString pwd = new SecureString();
            "password".ToCharArray().ToList().ForEach(c => pwd.AppendChar(c));
            Process p = new Process();
            psi.Password = pwd;
            p.StartInfo = psi;
            p.Start();
            p.WaitForExit();
        }
    }
}

Categories

Recent posts