Leviathan
Leviathan

Reputation: 948

Call Process in C# with String args[]

I want to call a Process from C# with multiple parameters.

When i call:

ProcessStartInfo info = new ProcessStartInfo();
...
info.Arguments = "argument";
Process.Start(info);

i can only set a String as attribute. (Same to all types of the Start method)

Is there a way to set a String[] as arguments or how does this String getting interpreted?

Because on the other side

static void Main(string[] args)

i am getting a String[].

Thanks in advance.

Upvotes: 1

Views: 2086

Answers (2)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186833

Technically you can do it like that:

  string[] args = new String[] {"argument1", "argument2", "argument3"};

  ...

  info.Arguments = String.Join(" ", args);

the restriction is that args should not have arguments with spaces

Upvotes: 3

Christos
Christos

Reputation: 53958

Is there a way to set a String[] as argument?

No you can't do this, since the type of ProcessStartInfo.Arguments is string. Hence you can assign to it an array of strings.

You could pass to this string the parameters as follows:

info.Arguments = "argument1 argument2 argument3";

and your .exe will be executed as you were passing an array of strings with elements (argument1,argument2,argument3).

Upvotes: 2

Related Questions