如何将参数传递给进程



有没有办法将字符串参数传递给从我自己的进程生成的进程。

我的主要应用程序中有:

Process.Start(Path.Combine(Application.StartupPath, "wow.exe");

wow.exe是我创建的另一个应用程序。我需要将参数传递给这个 exe(一个字符串(。我通常如何实现此目的?

我尝试过:

 ProcessStartInfo i = new //........
 i.Argument = "cool string";
 i. FileName = Path.Combine(Application.StartupPath, "wow.exe");
 Process.Start(i);

wow应用程序的主要内容中,我写道:

static void Main()
{
    //print Process.GetCurrentProcess().StartInfo.Argument;
}

但是我从来没有在第二个应用程序的 Main 中得到我的字符串。这是一个问why的问题,但没有how to solve it..

编辑:Environment.GetCommandLineArgs()[1],它必须是。尽管如此,还是让它工作了。接受了@Bali的回答,因为他首先提出了这个答案。谢谢大家

要传递参数,您可以在Main中使用string[] args,也可以使用 Environment.GetCommandLineArgs

例:

Console.WriteLine(args[0]);

Console.WriteLine(Environment.GetCommandLineArgs[0]);

你可能想要一个

static void Main(string[] args)
{
}

其中args包含您传入的参数

下面是如何将参数传递给 exe 的示例:

static void Main()
{
   string[] args = Environment.GetCommandLineArgs();
   string firstArgument = args[0];
   string secondArgument = args[1];
}

或者稍微改变一下你的主方法:

static void Main(string []args)
{}

在你的wow.exe程序中.cs

static void Main()
{
     //Three Lines of code 
}

将其更改为

static void Main(string[] args)
{
     //Three Lines of code 
}

string[] args.现在将包含传递给 exe 的参数。

或者您可以使用

string[] arguments = Environment.GetCommandLineArgs();

你的论点被空格" "打破了.

最新更新