Search code examples
c#.netparameter-passingexeelevated-privileges

Execute another EXE from an application with parameters, as admin


I have created two projects under the same solution. ProjectA is a Windows Form Application and ProjectB is a simple console application.ProjectB will be executed from ProjectA with admin privileges.
Sample from ProjectA

private void btnFinish_Click(object sender, EventArgs e)
        {
            ipAddress = txtIP.Text;
            bindingPort = txtPort.Text;
            if (!fileChosen)
            {
                CreateCertificate();
                //
            }
            //After this step i want to execute ProjectB with admin provileges with 3 parameters
            ExecuteB_AsAdminWithPrivileges(ipAddress, bindingPort, serverCert);
        }
    }

So when i click the button name Finish i want the ProjectB.exe to be executed with parameters that i will give from ProjectA.
And ProjectB will look sth like:

public static void StoreAndBindCertificate(string pfxFileServerCert, string ipAddress, string ipPort)
        {
//
}

This is the method which will be using the parameters from ProjectA.
How can i get the Parameters from ProjectA to this method in ProjectB?


Solution

  • You could use this method:

    public static int RunProcessAsAdmin(string exeName, string parameters)
    {
        try {
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.WorkingDirectory = CurrentDirectory;
            startInfo.FileName = Path.Combine(CurrentDirectory, exeName);
            startInfo.Verb = "runas";
            //MLHIDE
            startInfo.Arguments = parameters;
            startInfo.ErrorDialog = true;
    
            Process process = System.Diagnostics.Process.Start(startInfo);
            process.WaitForExit();
            return process.ExitCode;
        } catch (Win32Exception ex) {
            WriteLog(ex);
            switch (ex.NativeErrorCode) {
                case 1223:
                    return ex.NativeErrorCode;
                default:
                    return ErrorReturnInteger;
            }
    
        } catch (Exception ex) {
            WriteLog(ex);
            return ErrorReturnInteger;
        }
    }
    

    The first parameter will be your .exe file and the second one will be the parameters you want to give to your .exe file After this you should make changes in your .exe file in the main section. Something like:

    static void Main(string[] args)
            {
                if (args.Length <= 1) return;
    
                try
                {
                    if (args.Length == 2)
                    {
                        _IpAddress = args[0];
                        _IpPort = args[1];
                        FunctionName(_IpAddress, _IpPort);
                    }
                    else
                    {
                        _return
                    }
                }
                catch (Exception)
                {
                    throw new Exception("Invalid number of parameters!");
                }
            }
    

    I hope this helps.