Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

How to pass parameters to another process in c#

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

string [] args = {"a", "b"};
???????????? Process.Start ("C:   demo.exe" String.Join ("", args));

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...