Search code examples
c#.netprocess.net-coreargs

Pass args to Main(string[] args)


I am trying to build a Console Application to start my .NetCore Web Applications that I've got built as a .dll

Unfortunately the Args entered in the ProcessStartInfo are not being received by my application, however my application does start and i get a unexpected behaviour in the Console.WriteLine Method.

This code is inside my SocketAPI Project which is a .NetCore 2.2 WebApplication | API Project:

public static void Main(string[] args)
{
    // Outputs -> Received :
    // ?? Why is args.Length empty? Not even 0 or null??
    Console.WriteLine(string.Format("Received : ",args.Length));

    CreateWebHostBuilder(args).Build().Run();
}

It gets declared and called by my ProcessRunner which is a Class that holds the current Process:

I am also referring to this documentation: dotnet command documentation on microsoft.com

Which describes: dotnet [command] [arguments]

This Code is inside the ProcessRunner Constructor

ProcessStartInfo = new ProcessStartInfo
{
    FileName = "dotnet",
    Arguments = string.Format("BHR.{0}.dll {1}", Name, args),
    WorkingDirectory = ".\\",
    UseShellExecute = false,
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    CreateNoWindow = true,
};

Later on I'm calling Process.Start(); inside the ProcessRunner to start the Process.

As said I do get Output but no Args... So why do i see "Received :" but nothing on the end?

Do i have to enable my SocketAPI to receive args when starting them as a process? I've been trying to deal with the problem since 2 days now and I'm completely clueless...

Kind Regards


Solution

  • In your code you are not adding a placeholder for the argument length.

    Console.WriteLine(string.Format("Received: {0} ",args.Length));
    

    FYI, This works for me:

    var arguments = "myarg1 myarg2";
    var dir = @"C:\somedir\somechilddir";
    
    var info = new System.Diagnostics.ProcessStartInfo("dotnet", $"someproject.dll {arguments}");
    
    info.UseShellExecute = false;
    info.CreateNoWindow = true;
    info.WorkingDirectory = dir;
    
    var process = new System.Diagnostics.Process();
    process.StartInfo = info;
    process.Start();
    process.WaitForExit();
    

    Perhaps, try resolving your working directory using Path.GetFullPath();