首页 > 解决方案 > 如何在 VSCode 中通过调试/发布移动引用的 DLL?

问题描述

设置

我将 C# VS2015 控制台应用程序项目转换为 VSCode,并尝试添加 DLL 引用并在构建时移动任何引用的 DLL。

首先,Dotnet add package没有工作,错误The project does not support adding package references through the add package command.所以我手动将项目添加到.csproj文件中。我要添加的包是本地版本,位于./Lib/AutoItX

Lib/
   └── AutoItX
        ├── AutoItX3_x64.dll
        ├── AutoItX3.Assembly.dll
        ├── AutoItX3.Assembly.xml
        └── AutoItX3.dll

添加到.csprog

<ItemGroup>
    <Reference Include="AutoItX">
      <HintPath>.\Lib\AutoItX\AutoItX3.Assembly.dll</HintPath>
    </Reference>
  </ItemGroup>

该参考有效,如果我手动将 DLL 移动到 Debug 文件夹中,它会找到它并且一切都很好。

问题

有没有办法自动将 DLL 移动到文件夹中?

目前,重新运行后,我的调试文件夹如下所示:

bin/
   └── x64
        └── Debug
            ├── AutoItX3.Assembly.dll
            ├── AutoItX3.Assembly.xml
            ├── Application.exe
            ├── Application.exe.config
            └── Application.pdb

我希望AutoItX3_x64.dll在 x64 构建中自动包含,如果可能的话,AutoIt3.dll在 x86 构建中包含(它目前没有为 x86 构建设置,我只是想知道如何)

附加信息

我的launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "clr",
            "request": "launch",
            "preLaunchTask": "compile",
            "program": "${workspaceFolder}/Application/bin/x64/Debug/StartApplication.exe",
            "args": ["./Download/start.json"],
            "cwd": "${workspaceFolder}",
            "console": "externalTerminal",
            "stopAtEntry": false
        },
        {
            "name": ".NET Core Attach",
            "type": "clr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

我的tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "compile",
            "type": "shell",
            "command": "dotnet",
            "args": [
                // Ask msbuild to generate full paths for file names.
                "msbuild",
                "Application.sln",
                "/property:GenerateFullPaths=true",
                "/property:Platform=x64"
            ],
            "group": "build",
            "presentation": {
                // Reveal the output only if unrecognized errors occur.
                "reveal": "silent"
            },
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": "$msCompile"
        }
    ]
}

Application.sln


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Application", "Application\Application.csproj", "{E9AA3396-3EAD-47EE-9927-F20D87B34BF6}"
EndProject
Global
    GlobalSection(SolutionConfigurationPlatforms) = preSolution
        Debug|Any CPU = Debug|Any CPU
        Debug|x86 = Debug|x86
        Debug|x64 = Debug|x64
        Release|Any CPU = Release|Any CPU
        Release|x86 = Release|x86
        Release|x64 = Release|x64
    EndGlobalSection
    GlobalSection(ProjectConfigurationPlatforms) = postSolution
        {E9AA3396-3EAD-47EE-9927-F20D87B34BF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {E9AA3396-3EAD-47EE-9927-F20D87B34BF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
        {E9AA3396-3EAD-47EE-9927-F20D87B34BF6}.Debug|x86.ActiveCfg = Debug|x86
        {E9AA3396-3EAD-47EE-9927-F20D87B34BF6}.Debug|x86.Build.0 = Debug|x86
        {E9AA3396-3EAD-47EE-9927-F20D87B34BF6}.Debug|x64.ActiveCfg = Debug|x64
        {E9AA3396-3EAD-47EE-9927-F20D87B34BF6}.Debug|x64.Build.0 = Debug|x64
        {E9AA3396-3EAD-47EE-9927-F20D87B34BF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
        {E9AA3396-3EAD-47EE-9927-F20D87B34BF6}.Release|Any CPU.Build.0 = Release|Any CPU
        {E9AA3396-3EAD-47EE-9927-F20D87B34BF6}.Release|x86.ActiveCfg = Release|x86
        {E9AA3396-3EAD-47EE-9927-F20D87B34BF6}.Release|x86.Build.0 = Release|x86
        {E9AA3396-3EAD-47EE-9927-F20D87B34BF6}.Release|x64.ActiveCfg = Release|x64
        {E9AA3396-3EAD-47EE-9927-F20D87B34BF6}.Release|x64.Build.0 = Release|x64
    EndGlobalSection
    GlobalSection(SolutionProperties) = preSolution
        HideSolutionNode = FALSE
    EndGlobalSection
EndGlobal

我知道的和我尝试过的

microsoft 文档显示您可以使用 metatag <DependentUpon>,我尝试过但没有运气。
文档还提到了在 .else中使用 else-ware 的条件.csproj,这让我认为,一旦我有了一个可以工作的配置,我就可以创建一个条件,使其适用于两个平台。
似乎可以解决这个特定问题的另一件事是任务,但这感觉非常暴力,而且我不知道如何解析.csproj所有外部的本地 DLL 以移动。

标签: c#dllvisual-studio-codemsbuildcsproj

解决方案


这篇文章对解释如何链接内容非常有帮助。

<ItemGroup>
  <Content Include="..\Shared\SharedSettings.json" Link="SharedSettings.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

通过使用问题中链接文档中的条件,我能够分别移动 x86 和 x64 的 DLL。

这是最终解决方案.csproj

<ItemGroup>
    <Reference Include="AutoItX">
      <HintPath>$(ProjectDir)\Lib\AutoItX\AutoItX3.Assembly.dll</HintPath>
    </Reference>
    <Content Include="$(ProjectDir)\Lib\AutoItX\AutoItX3_x64.dll" Condition="'$(Platform)' == 'x64'" Link="AutoItX3_x64.dll" CopyToOutputDirectory="PreserveNewest" />
    <Content Include="$(ProjectDir)\Lib\AutoItX\AutoItX3.dll" Condition="'$(Platform)' == 'x86'" Link="AutoItX3.dll" CopyToOutputDirectory="PreserveNewest" />
  </ItemGroup>

推荐阅读