Home > Net >  C# passing array to array but still getting "cannot convert from 'string[]' to '
C# passing array to array but still getting "cannot convert from 'string[]' to '

Time:10-04

(using VS Community 2019 v16.10.4 on Win 10 Pro 202H)

I'm working on a C# console/winforms desktop app which monitors some local drive properties and displays a status message. Status message display is executed via Task Scheduler (the task is programmatically defined and registered). The UI is executed from the console app using the Process method. My intention is to pass an argument from Scheduler to the console app, perform some conditional logic and then pass the result to the UI entry (Program.cs) for further processing.

I’m testing passing an argument from the console app to the UI entry point and I’m getting a “Argument 1: cannot convert from 'string[]' to 'string'” error.

Code is:

class Program_Console
{
public static void Main(string[] tsArgs)
{
    // based on MSDN example
    tsArgs = new string[] { "Test Pass0", "TP1", "TP2" };
    Process p = new Process();
    try
    {
        p.StartInfo.FileName = BURS_Dir;
        p.Start(tsArgs); // error here
    }

public class Program_UI
{
[STAThread]
 public void Main(string[] tsArgs)
{

Isn’t "tsArgs" consistently an array?

CodePudding user response:

Start doesn' have a costractor with string array. if you look at msdn document youi will see that you can use something the closest to your example

public static  Start (string fileName, IEnumerable<string> arguments);

so you can try

p.Start( filename,tsArgs );

and replace filename with yours

CodePudding user response:

The only Start() method taking arguments as an array also needs the filename: Start(). You can't set the Filename via StartInfo and then omit that parameter in the method call.

The following should work for you:

p.Start(BURS_Dir, tsArgs);

CodePudding user response:

In .Net 5.0 , and .Net Core and Standard 2.1 , you can use a ProcessStartInfo for multiple command-line arguments

    tsArgs = new string[] { "Test Pass0", "TP1", "TP2" };
    Process p = new Process();
    try
    {
        p.StartInfo.FileName = BURS_Dir;
        foreach (var arg in tsArgs)
            p.StartInfo.ArgumentList.Add(arg);
        p.Start();
    }
    catch
    { //
    }

Alternatively, just add them directly

    Process p = new Process
    {
        StartInfo =
        {
            FileName = BURS_Dir,
            ArgumentList = { "Test Pass0", "TP1", "TP2" },
        }
    };
    try
    {
        p.Start();
    }
    catch
    { //
    }
  • Related