VS Code 在 VS Code 中运行或调试 C# 程序时如何设置参数

问题描述 投票:0回答:1

我尝试使用参数运行/调试的程序位于

${workspaceFolder}/src/GradeBook/Program.cs

看起来像这样:

using System;

namespace GradeBook {
    class Program {
        static void Main(string[] args) {
            String greeting = args.Length > 0 ? $"Hello {args[0]}!" : "Hello!";
            Console.WriteLine(greeting);
        }
    }
}
c# visual-studio-code command-line-arguments visual-studio-debugging vscode-debugger
1个回答
0
投票

在 VS Code 侧边栏中

  1. 单击“调试”图标
  2. 点击
    create a launch.json file
  3. 选择
    C#
    launch.json
    已创建并打开。
  4. 点击
    Add Configuration
  5. 选择
    {} .NET: Launch Executable file (Console)
  6. 删除
    "preLaunchTask": "build",
  7. 填写
    "program"
    路径
  8. 将参数添加到
    "args"
    ,例如
    "Jose Ville"

launch.json
看起来像这样:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "program": "${workspaceFolder}/src/bin/Debug/net8.0/GradeBook.dll",
            "args": ["Jose Ville"],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            "console": "internalConsole"
        }
    ]
}

现在,当您运行或调试程序时,

"Jose Ville"
作为参数传入,
"Hello Jose Ville!"
将被写入控制台。

© www.soinside.com 2019 - 2024. All rights reserved.