6

[Background Information]

So I installed the new Visual Studio 2022 and decided to try out .NET Core 6 for a simple script. Firstly, I created a new console app and was immediately confused. No main method, with no write up in the documentation to using CLI arguments (I need to pass two target path arguments). I found a overflow post regarding how to access arguments through the System.Environment class. So I moved on.

[Question]

In .NET Core 5 console apps I had no problem running compiled executables with arguments through the CLI. But when I published my code and ran it through the command line I got the following output (see output) with the strange line "program cannot run in DOS mode". Interestingly enough, it still reads one of ini files, but when I run through powershell it reads the other ini. This is very strange behavior to me. Should I just downgrade back to .NET Core 5 or try and understand what's happening? Is it the fact that I am reading INI's ? (unfortunately I have no choice as its a legacy project). Any help is appreciated.

[Code]

using System.IO;
//get command line arguments
try
{
    string stationIniPath = Environment.GetCommandLineArgs()[0];
    string equipmentIniPath = Environment.GetCommandLineArgs()[1];
    Console.WriteLine("- Reading INI data -");
    if(stationIniPath != null && equipmentIniPath != null){
        string[] stationINI = System.IO.File.ReadAllLines(stationIniPath);
        string[] equipmentINI = System.IO.File.ReadAllLines(equipmentIniPath);
        foreach(string station in stationINI)
        {
            Console.WriteLine(station);
        }
        foreach(string equipment in equipmentINI)
        {
            Console.WriteLine(equipment);
        }
    }
    else
    {
        throw new Exception("Need to specify command line arguments");
    }


}
catch (Exception e)
{
    Console.WriteLine("You must specify arguments [0] Station INI Path and [1] Equipment INI path :   " + e.Message);
}

[Output When Ran using Powershell & || CMD] CLI output

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 1
    Double check what the first argument passed into a CLI app is: https://learn.microsoft.com/en-us/dotnet/api/system.environment.getcommandlineargs?view=net-6.0#returns – rhughes Jan 11 '22 at 21:27
  • 2
    Regarding top level statements (no main method) and arguments, you can check here: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements#args – rhughes Jan 11 '22 at 21:29
  • 1
    Looks like the program is printing the content of its own executable. Probably indexes 0 and 1 should be 1 and 2. – Bent Tranberg Jan 11 '22 at 21:30
  • 1
    New console template uses .NET 5 feature - top level statements and for top level statements [`args` parameter is generated](https://stackoverflow.com/questions/70142512/net-6-0-c-sharp-new-console-template-how-to-read-cli-arguments/70142709#70142709) which you can use as was done in the old `Main`. – Guru Stron Jan 11 '22 at 21:30

2 Answers2

9

Environment.GetCommandLineArgs() always has process file name as the first element. You should start from index 1.

Berkays
  • 364
  • 3
  • 10
2

Another approach - use the args parameter generated by compiler:

The entry point method always has one formal parameter, string[] args. The execution environment creates and passes a string[] argument containing the command-line arguments that were specified when the application was started. The string[] argument is never null, but it may have a length of zero if no command-line arguments were specified. The args parameter is in scope within top-level statements and is not in scope outside of them. Regular name conflict/shadowing rules apply.

string stationIniPath = args[0];
string equipmentIniPath = args[1];
Guru Stron
  • 102,774
  • 10
  • 95
  • 132