NuGet package that copies files/folders to Output

You have the specific need: your NuGet package needs some native libs/files (not .NET assemblies) that need to place in the output folder.

For example, I have been using LEAD Tools to perform OCR to some images. The OCR engine needs a bunch of files to work (build and run, of course), so I created this structure inside my NuGet package:


Basically, if consists of a build folder (special folder) inside the package, with the file structure that you want to be copied into your output folder (where your executable files and/or libraries will be built).

And most importantly, it also needs to have a .targets file that is a special MSBuild file that will be interpreted each type you build your project.

In the snapshot above it’s listed as LeadTools.Ocr.AdvantageEngine-x86.targets

This file has to contain this script:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="">
    <NativeLibs Include="$(MSBuildThisFileDirectory)**\*.*" />
    <Content Include="@(NativeLibs)">

I’m not 100% sure, but I think this file should have the same name as your NuGet package (the Id of the package).

With both things, the files/folders inside the build folder and the targets file, you’re done 🙂

Installing a package like this in a project will modify your .csproj to include a special Import section like this:

  <Import Project="..\packages\LeadTools.Ocr.AdvantageEngine-x86.19.0.1\build\LeadTools.Ocr.AdvantageEngine-x86.targets" Condition="Exists('..\packages\LeadTools.Ocr.AdvantageEngine-x86.19.0.1\build\LeadTools.Ocr.AdvantageEngine-x86.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see The missing file is {0}.</ErrorText>
    <Error Condition="!Exists('..\packages\LeadTools.Ocr.AdvantageEngine-x86.19.0.1\build\LeadTools.Ocr.AdvantageEngine-x86.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\LeadTools.Ocr.AdvantageEngine-x86.19.0.1\build\LeadTools.Ocr.AdvantageEngine-x86.targets'))" />

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s