Search code examples
c#arraysconsolearguments

How to pass multiple argments with process.startInfo.Arguments


I have this peace of code here:

void Main(string[] args) {
   string[] ARRAY = new string[2];
   ARRAY[0] = "1";
   ARRAY[1] = "2";
   
   Process process = new Process();
   process.startInfo.FileName = @"PATH"; //runs itself
   process.startInfo.Arguments = ARRAY;
   process.Start();
}

I need the program to pass whole array to next instance itsself (it is able to receive), but i keep getting that it requires string. Is there any workaround?


Solution

  • process.startInfo.Arguments is of type string and you are trying to pass array of string to it.

    public string Arguments { get; set; }
    

    Instead of passing entire array, you have to convert it to the string first and then assign it to process.startInfo.Arguments

    ...
    process.startInfo.Arguments = string.Join(" ", ARRAY);
    

    Your final code will look like,

    void Main(string[] args) {
       string[] ARRAY = new string[2];
       ARRAY[0] = "1";
       ARRAY[1] = "2";
       
       Process process = new Process();
       process.startInfo.FileName = @"PATH"; //runs itself
       process.startInfo.Arguments = string.Join(" ", ARRAY);
       process.Start();            //^^^^^^^^^^^^^^^ This is what you need
    }
    

    For more details: string.Join()