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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16057063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:51:36  来源:igfitidea点击:

How to pass parameters to another process in c#

c#parametersprocess

提问by Ldg

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

但它们不能以其他形式提供。谢谢您的帮助

采纳答案by Robert

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

在演示.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.

等等。

回答by fhnaseer

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".

这里argumentOne、argumentTwo、argumentThree 将包含“a”、“b”和“c”。