How to pass parameters to an exe?

孤街醉人 提交于 2019-11-29 10:43:56
Mou

Hope the below code may help.

Code from first .exe:

Process p= new Process();
p.StartInfo.FileName = "demo.exe";
p.StartInfo.Arguments = "param1 param2";
p.Start();
p.WaitForExit();

or

Process.Start("demo.exe", "param1 param2");

Code in demo.exe:

static void Main (string [] args)
{
  Console.WriteLine(args[0]);
  Console.WriteLine(args[1]);
}

Right click on .exe file-->goto shortcut-->in target tab write the arguement in extreme right... in my case it worked

Btc Sources

You can see it in the following post (answer by @AndyMcCluggage):

How do I start a process from C#?

using System.Diagnostics;
...
Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = "process.exe";
process.StartInfo.Arguments = "-n";
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
process.WaitForExit();// Waits here for the process to exit.

It provides far more control as you can see in the MSDN, but basically the arguments control is quite easy as you can see, is just to modify a property with a string.

Update: Since with the snippet code above you would be starting PsExec, based on:

PsExec

The format you have to use is:

psexec @run_file [options] command [arguments]

Where: arguments Arguments to pass (file paths must be absolute paths on the target system).

Since the process you're starting is psexec, in the process.StartInfo.Arguments you would have to put all the parameters it would need, in a sigle chain: @run_file [options] command [arguments].

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!