1

I have an .exe app that I need to distribute with my C# app when it builds. I am trying to use Nuget to package it so that it will be included in the build root directory when building but am having trouble getting the behaviour I want.

Here is what I've got in my .nuspec file:

<?xml version="1.0"?>
<package>
  <metadata>
    <id>my.id</id>
    <version>1.0.0</version>
    <authors>me</authors>
    <owners>me</owners>
    <licenseUrl>myurl</licenseUrl>
    <projectUrl>myurl</projectUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>A copy of an .exe so we can easily distribute it 
       with our applications without needing to include it in our VCS repo</description>
    <releaseNotes>Initial test version</releaseNotes>
    <copyright>Copyright 2018</copyright>
    <dependencies>
    </dependencies>
    <packageTypes>
    </packageTypes>
    <contentFiles>
        <files include="any\any\myexe.exe" buildAction="None" copyToOutput="true" />
    </contentFiles>
  </metadata>
  <files>
    <file src="content\myexe.exe" target="content" />
  </files>
</package>

This puts the myexe.exe file to my VS project when I install the Nuget Package but it does not copy the file when I build. What I'd like is for the file to by installed with my other app files when building and to keep it out of my VS project.

I've been reading docs here but am not sure how to make the nuspec file.

More Details:

Nuget 4.5.1

Visual Studio 2015

Note: the <files> and <contentFiles> might seem to be duplicating functionality. I'd like to employ both as I understand this will future-proof it for VS2017

Ryan Southcliff
  • 143
  • 2
  • 12

1 Answers1

3

Nuget: Including an exe as a Run-time dependency

First, I know you want to use some technologies for the future, but we have to know that these future-oriented technologies often have certain constraints and conditions.

For example, <contentFiles> is used for NuGet 4.0+ with PackageReference, neither of them is supported by Visual Studio 2015. See Using the contentFiles element for content files for some details.

If you are interested in the <contentFiles>, you can read the blog NuGet is now fully integrated into MSBuild.

Go back to our question now, according to above info, we should not use <contentFiles> when we use Visual Studio 2015. To resole this issue, we need to add a .targets file in the nuget package when you build the project:

The content of .targets file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="$(ProjectDir)myexe.exe">
      <Link>myexe.exe</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CustomToolNamespace></CustomToolNamespace>
    </None>
  </ItemGroup>
</Project>

The .nuspec file like following:

  <files>
    <file src="build\YouNuGetPackageName.targets" target="build\YouNuGetPackageName.targets" />
    <file src="content\myexe.exe" target="content\myexe.exe" />
  </files>

Note: The name of the .targets file should be same as your nuget package name.

With this way, when you build your project, MSBuild/VS would copy the file myexe.exe to the output folder.

Besides, if you want to copy the file myexe.exe to other destination, you can replace the content of .targets file with a copy task, like:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <Target Name="CopyMyexe" BeforeTargets="Build">
  <Message Text="Copy CopyMyexe to the folder."></Message>
  <Copy
  SourceFiles="$(ProjectDir)myexe.exe"
  DestinationFolder="xxx\xxx\xx\myexe.exe"
/>
  </Target>
</Project>

See Creating native packages and similar issue for some helps.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 1
    Great Answer. +1. This works and I am just reading up to better understand it now. One issue though: I'd like to keep myexe.exe out of my VS project if possible. The ideal process being that when I install this Nuget Package myexe.exe does not get added to the project. Only when I build the file gets added to the build folder. My thinking is that with this, I do not need to save the .exe file in our Version Control but can trust that it will automatically be included if a colleague pulls down my repo because of the Nuget reference. Perhaps `content` is the wrong type of folder? – Ryan Southcliff Aug 15 '18 at 14:15
  • @RyanSouthcliff, yes, you should use `Tools` type of folder. This is what you want. Besides, since above answer resolved your question, you could accept it as answer, this will be beneficial to other community members who has the same issue to read this thread and easy to find answer. – Leo Liu Aug 15 '18 at 14:42