如何更改 Visual Studio 代码中的默认构建输出目录

How can I change the default build output directory in Visual Studio Code

我是 Visual Studio 代码的新手。我想在我的 csproj 中定义输出目录。我不知道在哪里更改 Visual Studio 编码目标文件夹以找到生成的 dll 文件。

VSCode 使用 dotnet CLI,特别是用于构建 dotnet build 命令。其中,它有以下选项

-o|--output <OUTPUT_DIRECTORY>
Directory in which to place the built binaries.

假设您的构建任务在 .vscode/tasks.json 文件中定义:

{
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "build",
            "command": "dotnet build",
            ...
        }
    ]
}

您可以使用所需路径添加 -o arg。例如,更改为:

...
"command": "dotnet build -o ${workspaceRoot}/bin/another_Debug/",
...

其中 ${workspaceFolder} 是 VSCode 之一 predefined variables:

${workspaceFolder} the path of the workspace folder that contains the tasks.json file

如果您想要 VSCode、VS 和 dotnet CLI 的一致行为,我建议编辑 csproj 文件以通过添加以下内容来设置输出路径:

<PropertyGroup>
  <OutputPath>..\custom-output-dir\</OutputPath>
  <!-- Remove this if you multi-target and need the created net*, netcoreapp* subdirs -->
  <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

第二个 属性 阻止 SDK 添加目标框架的子目录,这是多目标构建所必需的,但如果您只针对单个框架(=当您只有一个TargetFramework 属性).

设置路径时还可以使用其他属性,例如:

<OutputPath>..\custom-output-dir$(Configuration)$(MSBuildProjectName)\</OutputPath>

假设您的项目是 MyProj.csproj,那么输出路径将是 ..\cusotm-output-dir\Debug\MyProj\