Search code examples
c#dllvisual-studio-codemsbuildcsproj

How do i move referenced DLLs with debug/release in VSCode?


Set up

I converted a C# VS2015 console application project over to VSCode and im trying to add a DLL reference and move any referenced DLLs on build.

To start off, Dotnet add package didnt work, with the error The project does not support adding package references through the add package command. so i added the project manually to the .csproj file. The package i want to add is a local version, located at ./Lib/AutoItX

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

additions to .csprog

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

The reference works, and if i manually move the DLL into the Debug folder, it finds it and all is well.

Question

Is there a way to automate moving the DLL into the folder?

currently, after a fresh run, my debug folder looks as followed:

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

i would like the AutoItX3_x64.dll included automatically on x64 builds, and if possible, the AutoIt3.dll included on x86 builds (its not currently set up for x86 builds, i just want to know how)

Additional Information

my 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}"
        }
    ]
}

my 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

What I know and what I've tried

microsofts docs show that you can use a metatag <DependentUpon>, which i tried with no luck.
The docs also mention conditions which are used else-ware in the .csproj, which make me think that once i have one configuration that works, i can make a condition to make it work for both platforms.
One more thing that seems like it could solve this specific issue is tasks, but this feels very brute-force-ish, and i wouldn't know how to parse the .csproj for all external, local DLLs to move.


Solution

  • this post was really helpful in explaining how to link content.

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

    By using the conditionals from the linked documentation in the question, I was able to move the DLLsfor x86 and x64 respectively.

    This was the final solution for the .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>