当我尝试启动 C# 代码时收到消息“找不到任务‘构建’”

Joy*_*ode 12 c# .net-core

我是 C# 新手,正在学习使用复数

我完全遵循了教程正在做的事情,但遇到了教程没有的问题。

当您通过 .Net 编写 C# 代码时,我构建了简单的“Hello World”程序

在Visual Studio Code中,如果选择Start Debugging或Start without Debugging,会弹出如下错误

''找不到任务 'build'"

然后我还是点击调试然后出现以下错误

launch:program dir \ < insert-project-name-here >.dll不存在有打开launch.json的选项

令人困惑的是,.dll 文件确实存在......

此外,检查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",
      "WARNING01": "*********************************************************************************",
      "WARNING02": "The C# extension was unable to automatically decode projects in the current",
      "WARNING03": "workspace to create a runnable launch.json file. A template launch.json file has",
      "WARNING04": "been created as a placeholder.",
      "WARNING05": "",
      "WARNING06": "If OmniSharp is currently unable to load your project, you can attempt to resolve",
      "WARNING07": "this by restoring any missing project dependencies (example: run 'dotnet restore')",
      "WARNING08": "and by fixing any reported errors from building the projects in your workspace.",
      "WARNING09": "If this allows OmniSharp to now load your project then --",
      "WARNING10": "  * Delete this file",
      "WARNING11": "  * Open the Visual Studio Code command palette (View->Command Palette)",
      "WARNING12": "  * run the command: '.NET: Generate Assets for Build and Debug'.",
      "WARNING13": "",
      "WARNING14": "If your project requires a more complex launch configuration, you may wish to delete",
      "WARNING15": "this configuration and pick a different template using the 'Add Configuration...'",
      "WARNING16": "button at the bottom of this file.",
      "WARNING17": "*********************************************************************************",
      "preLaunchTask": "build",
      "program": "${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll",
      "args": ["link"],
      "cwd": "${workspaceFolder}",
      "console": "internalConsole",
      "stopAtEntry": false
    },
    {
      "name": ".NET Core Attach",
      "type": "coreclr",
      "request": "attach",
      "processId": "${command:pickProcess}"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*lor 13

简单的方法

在 VS Code 中,加载代码库后。调出命令托盘并回车,> .NET Generate Assets for Build and Debug然后按回车/回车。

这将强制 VS Code 重新创建launch.jsontask.json文件。

留意 VS Code 窗口的左下角是否有一条吐司消息,如下所示:

VS Code 的屏幕截图,显示了在重新创建构建和调试资产时要求确认的 Toast 消息

并确保您单击 Yes

复杂的方式

您需要更改task.json文件中当前读取的行:

"program": "${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll",
Run Code Online (Sandbox Code Playgroud)

替换<insert-target-framework-here>为 csproj 中的目标框架名称,它可能类似于以下内容:

<TargetFramework>netcoreapp2.1</TargetFramework>
Run Code Online (Sandbox Code Playgroud)

您还需要将<insert-project-name-here>.dll字符串替换为生成的 DLL 的名称。

  • 我收到以下消息“无法找到 .NET Core 项目”。选择“生成用于构建和调试的资产”选项后,未生成资产...:S (7认同)
  • @JoyFulCode您需要重新启动VS Code,然后它就会工作。 (2认同)

Sar*_*ahB -1

在 Visual Studio 上,您可以使用 ac# 扩展的生成器来构建 launch.json 和 task.json 但是,您还应该使用 C# 扩展的生成器来生成 launch.json 和task.json。阅读此内容:在这里执行此操作。但是,如果您是 C# 初学者,最好使用 Visual Studio。所有工具均已集成。它不是编辑文本,而是 IDE。但如果您更喜欢 Visual Studio 代码,请阅读我发送的文档:)。 视觉工作室IDE