为什么我的 .NET Core 6 控制台应用程序不想在 CLI 中 运行?

Why does my .NET Core 6 console app not want to run in CLI?

[背景信息]

所以我安装了新的 Visual Studio 2022 并决定尝试使用 .NET Core 6 作为一个简单的脚本。首先,我创建了一个新的控制台应用程序并立即感到困惑。没有主要方法,文档中也没有写到使用 CLI 参数(我需要传递两个目标路径参数)。我发现了关于如何通过 System.Environment class 访问参数的溢出 post。所以我继续前进。

[问题]

在 .NET Core 5 控制台应用程序中,我没有遇到任何问题 运行通过 CLI 使用参数编译可执行文件。但是,当我通过命令行发布我的代码并 运行 它时,我得到了以下输出(参见输出),其中包含 st运行ge 行“程序无法在 DOS 模式下 运行”。有趣的是,它仍然读取一个 ini 文件,但是当我通过 powershell 运行 它读取另一个 ini。这对我来说是非常 st运行ge 的行为。我应该降级回 .NET Core 5 还是尝试了解发生了什么?我正在阅读 INI 的事实吗? (不幸的是,我别无选择,因为它是一个遗留项目)。感谢任何帮助。

[代码]

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

[当 运行 使用 Powershell & || 时的输出命令]

Environment.GetCommandLineArgs() 始终将进程文件名作为第一个元素。您应该从索引 1 开始。

另一种方法 - 编译器使用 args 参数 generated

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