1

I have a html file included in my nuget package. But how can i then reference and read from that file in the nuget project?


What i tried so far

placed .html file in a contentFiles dir in the project.

enter image description here

included it in the nuget package.

enter image description here

contentFiles here contains the .html file.

enter image description here

Now i want to be able to read the contents of that html file from inside my nuget package.

Here is some example code that does not work.

        var path = Assembly.GetExecutingAssembly().Location.Replace("Ssff.dll", "Inlined.html");
        var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        var textPath = Path.Combine(assemblyDirectory, "contentFiles", "Inlined.html");
        
        // File.ReadAllText(path) and File.ReadAllText(textPath); does not work

Once this nuget package is consumed and tries to runs this code, it cannot find the html file. So the previous code if not uncommented just throws a file not found exception. So this whole thing boils down to how do i find that html file included in my nuget package.

This is what it looks like in the project consuming the nuget package. Note that there isnt a .html file or a contentFiles folder.

enter image description here

the PackageReference looks like this

<PackageReference Include="Ssff" Version="0.9.4.4">
    <IncludeAssets>all</IncludeAssets>
</PackageReference>
Misk
  • 157
  • 1
  • 2
  • 15
  • 3
    How about `true` in your package library's .csproj, instead of the `` tag? See [docs on packing via MSBuild](https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#including-content-in-a-package), specifically where it says "Other pack specific metadata that you can set on any of the above items includes and which sets CopyToOutput and Flatten values on the contentFiles entry in the output nuspec.". – Sean Skelly Aug 13 '21 at 21:13
  • That did the trick! – Misk Aug 14 '21 at 12:13

2 Answers2

2

In the .csproj for the package library, change <CopyToOutput>true</CopyToOutput> to <PackageCopyToOutput>true</PackageCopyToOutput> for the desired Content files.

Per documentation on NuGet packing via MSBuild:

Other pack specific metadata that you can set on any of the above items includes <PackageCopyToOutput> and <PackageFlatten> which sets CopyToOutput and Flatten values on the contentFiles entry in the output nuspec.

Translation: <PackageCopyToOutput> will tell consumers of this NuGet package to copy the desired Content files to their output, whereas <CopyToOutput> is not 'passed through' to the consuming projects.

Sean Skelly
  • 1,229
  • 7
  • 13
0

File.ReadAllText(path) and File.ReadAllText(textPath); does not work

With these functions you can read any file present on the disk, you just need to specify the good path that seem you don't do.

And when you said it don't work, what append ? An System.IO error or just nothing is returned ?

  • Yeah I know. It is the path for that included file I cannot find. It just throws a file not found exception – Misk Aug 09 '21 at 14:05
  • If that a nuget package, it should be a child of the 'packages' folder at the root of your project EDIT: If you add it manually (Not downloaded when you add the nuget package), it will be at is orignal path not on the nuget package as it don't come from it – 0x0000000000000 Aug 09 '21 at 14:15
  • a similar issue was discussed here https://github.com/NuGet/Home/issues/9297 – aamd Aug 18 '21 at 15:56