如何从 nuget 包中包含的内容文件中读取文本
How to read text from an included content file in a nuget package
我的 nuget 包中包含一个 html 文件。
但是我如何才能在 nuget 项目中引用和读取该文件?
到目前为止我尝试了什么
将 .html 文件放在项目的 contentFiles 目录中。
包含在 nuget 包中。
此处的 contentFiles 包含 .html 文件。
现在我希望能够从我的 nuget 包中读取 html 文件的内容。
这是一些不起作用的示例代码。
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
使用此 nuget 包并尝试运行此代码后,它找不到 html 文件。所以前面的代码如果没有取消注释只会抛出一个文件未找到异常。所以这整件事归结为我如何找到我的 nuget 包中包含的 html 文件。
这是使用 nuget 包的项目中的样子。请注意,没有 .html 文件或 contentFiles 文件夹。
PackageReference 看起来像这样
<PackageReference Include="Ssff" Version="0.9.4.4">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
File.ReadAllText(path) and File.ReadAllText(textPath); does not work
使用这些函数,您可以读取磁盘上存在的任何文件,您只需要指定您似乎没有指定的好路径。
当你说它不起作用时,附加什么? System.IO 错误或什么也没有返回?
在包库的 .csproj 中,将所需内容文件的 <CopyToOutput>true</CopyToOutput>
更改为 <PackageCopyToOutput>true</PackageCopyToOutput>
。
根据 NuGet packing via MSBuild 上的文档:
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.
翻译:<PackageCopyToOutput>
将告诉此 NuGet 包的消费者将所需的内容文件复制到他们的输出,而 <CopyToOutput>
不是 'passed through' 到消费项目。
我的 nuget 包中包含一个 html 文件。 但是我如何才能在 nuget 项目中引用和读取该文件?
到目前为止我尝试了什么
将 .html 文件放在项目的 contentFiles 目录中。
包含在 nuget 包中。
此处的 contentFiles 包含 .html 文件。
现在我希望能够从我的 nuget 包中读取 html 文件的内容。
这是一些不起作用的示例代码。
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
使用此 nuget 包并尝试运行此代码后,它找不到 html 文件。所以前面的代码如果没有取消注释只会抛出一个文件未找到异常。所以这整件事归结为我如何找到我的 nuget 包中包含的 html 文件。
这是使用 nuget 包的项目中的样子。请注意,没有 .html 文件或 contentFiles 文件夹。
PackageReference 看起来像这样
<PackageReference Include="Ssff" Version="0.9.4.4">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
File.ReadAllText(path) and File.ReadAllText(textPath); does not work
使用这些函数,您可以读取磁盘上存在的任何文件,您只需要指定您似乎没有指定的好路径。
当你说它不起作用时,附加什么? System.IO 错误或什么也没有返回?
在包库的 .csproj 中,将所需内容文件的 <CopyToOutput>true</CopyToOutput>
更改为 <PackageCopyToOutput>true</PackageCopyToOutput>
。
根据 NuGet packing via MSBuild 上的文档:
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.
翻译:<PackageCopyToOutput>
将告诉此 NuGet 包的消费者将所需的内容文件复制到他们的输出,而 <CopyToOutput>
不是 'passed through' 到消费项目。