Sta*_*und 6 .net c# console-application .net-6.0
【背景资料】
因此,我安装了新的 Visual Studio 2022,并决定尝试使用 .NET Core 6 来编写一个简单的脚本。首先,我创建了一个新的控制台应用程序,但立即感到困惑。没有 main 方法,文档中也没有使用 CLI 参数的内容(我需要传递两个目标路径参数)。我发现了一篇关于如何通过 System.Environment 类访问参数的溢出帖子。所以我继续前进。
[问题]
在 .NET Core 5 控制台应用程序中,我通过 CLI 运行带参数的已编译可执行文件没有问题。但是,当我发布代码并通过命令行运行它时,我得到了以下输出(请参阅输出),其中包含奇怪的行“程序无法在 DOS 模式下运行”。有趣的是,它仍然读取其中一个 ini 文件,但是当我运行 powershell 时,它会读取另一个 ini 文件。这对我来说是非常奇怪的行为。我应该降级回 .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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2822 次 |
| 最近记录: |