且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c#中如何将参数传递给另一个进程

更新时间:2023-02-14 16:24:36

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

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

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

并在主

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