0

(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?

EDIT: For clarity I’m using .NET Framework 4.7.2. The problem was not with consistency of what I am passing but in the Process.Start(String, IEnumerable String) overload. I believed “IEnumerable String” included string[ ]; it obviously does not since I was able to pass a plain string (not a string variable -- that also failed – just a hardcoded string).

In case it’s useful to somebody, my work-around is saving the arguments to a SQLite table in the console app and loading them into a List in the UI app. I’m sure a more proficient programmer could do it more efficiently.

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

I could not get this t work; same error. However I did get "Process.Start(BURS_Dir, "Test Pass0");" to successfully pass the string argument. Maybe it's not possible to pass an array with Process?
Ok, then you're using .NET Framework and not .NET Core. .NET Framework does not have that method. Which example did you use as your basis?
Yes I'm using .NET Framework 4.7.2. I followed the MSDN examples at learn.microsoft.com/en-us/dotnet/api/…. See my edit for functioning work-around. Thx for trying.
@ArtHansen: none of the examples over there uses an array for the arguments.
0

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
    { //
    }

2 Comments

not sure why but I get 'ProcessStartInfo' does not contain a definition for 'ArgumentList' error when doing a copy/paste; changing ArgumentList to Arguments did not help (I presume .NET Framework 4.7.2 satisfies the "Standard 2.1+" requirement).
No it doesn't actually. .NET Framework is not .NET Standard. You'll just have to join all the strings together yourself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.