You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code中运行C#?settings.json需要配置哪些内容?

基础检查
  • 先确认你已经安装了对应版本的.NET SDK,以及VS Code官方的 C# Dev Kit 插件,这是运行C#代码的前提
  • 必须打开C#项目的根目录(包含.csproj文件的目录)运行,单独打开单个.cs文件无法正常触发运行逻辑,会自动跳转settings.json
settings.json 必要配置

将以下配置项添加到你的settings.json中即可,Windows下的dotnet路径默认如下,macOS或Linux用户自行替换为dotnet的实际安装路径:

{
    // 配置dotnet可执行文件路径
    "dotnet.dotnetPath": "C:\\Program Files\\dotnet\\dotnet.exe",
    // 用C# Dev Kit默认语言服务,关闭旧版Omnisharp
    "dotnet.server.useOmnisharp": false,
    // 调试输出到VS Code内置终端
    "csharp.debug.console": "integratedTerminal",
    // 关闭调试出错时自动打开settings.json的逻辑
    "debug.openSettingsOnError": "off"
}
补充配置(可选,仅上述配置无效时添加)

如果添加以上配置后仍无法运行,你需要在项目根目录的.vscode文件夹下新增两个配置文件:

1. tasks.json(编译任务配置)

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/${workspaceFolderBasename}.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        }
    ]
}

2. launch.json(启动参数配置)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // 注意把下方的<你的.NET版本号>替换为实际使用版本,比如net6.0、net8.0
            "program": "${workspaceFolder}/bin/Debug/<你的.NET版本号>/${workspaceFolderBasename}.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal",
            "stopAtEntry": false
        }
    ]
}

配置完成后重启VS Code即可正常运行C#代码。

内容的提问来源于stack exchange,提问作者anarmeen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 21:27:03