0

Using the new Microsoft.NET.Sdk project format, I'd like to achieve the following for a class library packed and consumed by a .Net application:

  • have a text file (say info.txt) as part of project A (class library)
  • when project A is packaged (typically with 'dotnet pack'), info.txt is included as part of the package
  • when project A nuget package is included in another project (say .Net 5 application), info.txt is always included in the bin folder (irrespective of debug/release etc) when built and deployed.

Obviously the intent with this is that the consuming project doesn't need to be aware of the txt file so it's oblivious to the process.

I've seen one potential solution that uses an install.ps.

One that uses PackagePath but while I can get the txt file packaged, it's not clear on how to ensure this is copied to the bin.

Also, having just ported my library to the new project format, I'm seeing a confusing mix of nuspec and csproj concepts which is making it difficult to understand if this is even achievable in the new format.

Phil Cooper
  • 3,083
  • 39
  • 63

1 Answers1

0

Install scripts are unreliable as they do not run in certain scenarios, but you can use content files.

You can specify this in your csproj file. Add the info.txt file to your project and set its build action to None or Content. In your project file, add the Pack and attribute and the PackageCopyToOutput tag like below.

<None Include="info.txt" Pack="true">
   <PackageCopyToOutput>true</PackageCopyToOutput>
</None>

The Pack attribute will include the file in the package as a content file and PackageCopyToOutput set to true will cause the file to be copied to the output folder of the consuming project.

thatguy
  • 21,059
  • 6
  • 30
  • 40
  • Thanks for your suggestion and pleased you mentioned the install scripts are unreliable - this wasn't a road I wanted to go down. I've tried this by adding the csprojs changes - while the text file appears in the nupkg, when I add the package to an existing application and build, it doesn't appear in the bin directory. I also tried publishing but still didn't appear, i'm i missing something? – Phil Cooper Jul 22 '20 at 12:05
  • @PhilCooper Do you use `packages.config` or `PackageReference` in the consuming project? – thatguy Jul 22 '20 at 15:57
  • Packages.config - it's an MVC5 application. Do you think converting that to .net core with the newert project format would help? – Phil Cooper Jul 23 '20 at 09:45
  • Ok, this approach does not work with `packages.config`. If you can convert to the new project format it will work. However, if you can't and you want to do this reliably for both `packages.config` and `PackageReference`, you will need to add a custom build script like for example in this [post](https://stackoverflow.com/questions/37493187/is-there-an-alternative-to-contentfiles-with-projects-that-use-packages-config), because `contentFiles` are not supported in this case. I suggest you to migrate since the other option requires much more effort an cannot be done directly in the `csproj` file. – thatguy Jul 23 '20 at 10:47