2012-05-21 130 views
1

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

我有我的主要应用:

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

但我从来没有得到我的字符串中有第二个应用程序的主。 Here is a question,询问why,但没有how to solve it ..

编辑:Environment.GetCommandLineArgs()[1],它必须是。尽管如此,它的工作。接受@巴厘岛的答案,他首先与这个答案齐聚一堂。感谢所有

回答

2

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

例子:

Console.WriteLine(args[0]); 

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

在你wow.exe的Program.cs

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

改变它

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

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

或者你可以使用

string[] arguments = Environment.GetCommandLineArgs(); 

你的论点是由空间" "打破。

+0

ARGS []传递的参数?如何获得? – nawfal

+0

我在Main()中看不到任何类似的东西。 – nawfal

+0

@nawfal:现在检查 –

1

下面是一个例子,说明你可以得到参数传递给你的EXE:

static void Main() 
{ 
    string[] args = Environment.GetCommandLineArgs(); 

    string firstArgument = args[0]; 
    string secondArgument = args[1]; 
} 

或更改你的主要方法的位:

static void Main(string []args) 
{} 
2

你可能需要一个

static void Main(string[] args) 
{ 
} 

其中args包含您在

+0

我试过这个,但是我仍然得到了'Process'的空字符串。GetCurrentProcess()。StartInfo.Argument;'主要 – nawfal

+0

你不需要那个。尝试'Console.WriteLine(args [0] .ToString())' – MichelZ