Share via


Nuget package to copy exe content file to project output directory

Question

Tuesday, September 10, 2019 8:32 PM

I'm attempting to create a Nuget package that will copy an executable file to the output directory of a .Net framework web project.

Here is my nuspec file:

<?xml version="1.0"?> 
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
    <metadata> 
        <id>CopyExeToOutputNugetPackage</id> 
        <version>1.0.0</version> 
        <authors>Some Dude</authors>
        <owners>Some Owner</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance> 
        <description>A package to copy an exe to the output directory.</description> 
        <tags>CopyExeToOuput</tags> 
        <contentFiles> 
            <files include=".\content\test.exe" buildAction="None" copyToOutput="true" />           
        </contentFiles> 
    </metadata>
</package>

The "nuget pack" command works fine and builds my .nupkg file. I can then add the nuget project to my .Net Framework project and the test.exe file is added to my project, this is the entry in the .csproj file:

<ItemGroup>
  <Content Include="test.exe" />
</ItemGroup>

I can then use Visual Studio to edit the file properties to copy to the output directory and my project file is updated:

<ItemGroup>
  <Content Include="test.exe">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </Content>
</ItemGroup>

I would prefer that this last manual step is not required.

I've attempted to use a .targets file but that's either the wrong thing to do (because there is no compilation and msbuild is not involved) or I never got the configuration correct.

I've also tried using the nuspec files element:

<files>
    <file src="test.exe" target="lib\net462" />
</files>

With this last files element configuration, I get the following exception when attempting to add the nuget package to my .Net Framework v4.6.2 project:

Failed to add reference to 'test'. Please make sure that the file is accessible, and that it is a valid assembly or COM component.

Is there a way for me to specify in the .nuspec file that the contentFiles should be copied to the output directory? It seems like the copyToOutput="true" attribute should do the job but that does not appear to be the case.

All replies (2)

Wednesday, September 11, 2019 7:58 AM âś…Answered

Hi Dudeman3000,

Welcome to MSDN forum.

As far as I know, the "copyToOutput" property often is used for .NET Core. So we recommend you could add a PowerShell script into nuget package, when we install the nuget package, the script would run automatically to modify this property. Please follow the below steps:

#1 Please run below command in power shell to trust your script:

set-executionpolicy remotesigned

#2 download PowerShell for visual studio 2017/2019

#3 create text file and type below code, then rename "Intall1.ps1" to modify the type of file

param($installPath, $toolsPath, $package, $project)

$file1 = $project.ProjectItems.Item("test.exe")

$copyToOutput1 = $file1.Properties.Item("CopyToOutputDirectory")
$copyToOutput1.Value = 1 

#4 add the below code into your nuspec file

<files>
    <file src="content\test.exe" target="content\test.exe" />
    <file src="Install.ps1" target="tools\Install.ps1" />
</files>

#5 generate the nuget package

#6 then install the nuget packge in VS. You could check if the script run in output window.

Any feedback will be expected.

Best Regards,

Dylan

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected]


Wednesday, September 11, 2019 9:31 PM

This worked out very nicely! Thanks Dylan.

One minor note that might help others out, I nested the executable in another "ParentFolder" folder, so the ProjectItems line in my Install.ps1 PowerShell script looks like this now:

$file1 = $project.ProjectItems("ParentFolder").ProjectItems("test.exe")