以管理员身份从带有参数的应用程序执行另一个 EXE

Execute another EXE from an application with parameters, as admin

我在同一个解决方案下创建了两个项目。 ProjectA 是一个 Windows 表单应用程序,而 ProjectB 是一个简单的控制台 application.ProjectB 将从具有管理员权限的 ProjectA 执行。
来自项目 A 的示例

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);
        }
    }

因此,当我单击按钮名称“完成”时,我希望 ProjectB.exe 使用我将从 ProjectA 提供的参数执行。
ProjectB 看起来像:

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

这是将使用 ProjectA 中的参数的方法。
如何从 ProjectA 获取参数到 ProjectB 中的此方法?

更新

ProgramA{
string ip ="123.123.123";
File.WriteAllText("c://MtDataFromA.txt","ip="+ip);
}


private void btnFinish_Click(object sender, EventArgs e)
            {
                ipAddress = File.WriteAllText("c://MtDataFromA.txt");//some algorithem to find the ip from text file

    }


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


        // Use ProcessStartInfo class
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.FileName = "YourFile.exe";
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.Arguments = "ipAddress"+" " +"ipPort";

        try
        {
            // Start the process with the info we specified.
            // Call WaitForExit and then the using statement will close.
            using (Process exeProcess = Process.Start(startInfo))
            {
                exeProcess.WaitForExit();
            }
        }
catch
        {
             // Log error.
        }
}

link

你可以使用这个方法:

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;
    }
}

第一个参数是您的 .exe 文件,第二个参数是您要给 .exe 文件的参数 在此之后,您应该在 main 部分的 .exe 文件中进行更改。 类似于:

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!");
            }
        }

希望对您有所帮助。