How to pass parameters to another process in c#

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

I just created an application that launches processes with the following code

I would like to be able to pass the parameters from this application to the process I've launched.

where I have to enter the parameters in the project of the process that I've launched? I tried to put them in

static void Main (string [] args) {... 

but they are not available in other forms. thanks for the help

回答1:

Process p= new Process(); p.StartInfo.FileName = "demo.exe"; p.StartInfo.Arguments = "a b"; p.Start(); 

or

Process.Start("demo.exe", "a b"); 

in demo.exe

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

You asked how to save these params. You can create new class with static properties and save these params there.

class ParamHolder {   public static string[] Params { get; set;} } 

and in main

static void Main (string [] args) {   ParamHolder.Params = args; } 

to get params in any place of your program use:

Console.WriteLine(ConsoleParamHolder.Params[0]); Console.WriteLine(ConsoleParamHolder.Params[1]); 

etc.



回答2:

Main Application code:

var process = new Process(); process.StartInfo.FileName = "demo.exe" // Path to your demo application. process.StartInfo.Arguments = "a b c"   // Your arguments process.Start(); 

In your demo application (I am assuming that this is console application)

static void Main (string [] args) {     var agrumentOne = args[0];     var agrumentTwo = args[1];     var agrumentThree = args[3]; } 

Here argumentOne, argumentTwo, argumentThree will contain "a", "b" and "c".



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