c#中如何将参数传递给另一个进程 [英] How to pass parameters to another process in c#

查看:32
本文介绍了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

推荐答案

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

这篇关于c#中如何将参数传递给另一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆