简体   繁体   中英

Start a process with a command line parameter of another executable

I'm trying to pass an .exe to another through C# code.

Here's my code so far:

string ex1 = System.Windows.Forms.Application.StartupPath.ToString() + "\\dev\\psm.exe";
string ex2 = System.Windows.Forms.Application.StartupPath.ToString() + "\\dev\\Application\\app.exe";

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = ex1;
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.Arguments = ex2;

try
{
    Process.Start(startInfo);
}
catch
{
}

Which argument would work as dragging a file onto the application?
Details:
When you run psm.exe normally, it prompts for file name and directory.
运行psm.exe


However, when you drag an approved app on psm.exe, 使用psm.exe运行文件
it loads the app automatically. 正在运行的应用



How can this be done with C#?

You can run another app synchronously like this:

System.Diagnostics.Process myapp = new System.Diagnostics.Process();
myapp.StartInfo.FileName = ex1;
myapp.StartInfo.Arguments = ex2;
myapp.Start();
myapp.WaitForExit();

Depending on how the app you want to launch expects the command line arguments to be passed, you may need this for the arguments:

myapp.StartInfo.Arguments = String.Format("/MyArgumentName={0}", ex2);

That would be the equivalent of:

c:\MyApplicationStartupPath\dev\psm.exe /MyArgumentName=c:\MyApplicationStartupPath\Application\App.exe

Be sure to match the way app.exe expects the parameters in your StartInfo.Arguments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM