.NETCore Nuget 发布包含静态文件 content file

发布时间 2023-12-21 17:52:29作者: WebEnh

.NETCore 在.csproj引用资源中标记pack配置

<pack>true</pack>
1
例如

<ItemGroup>
<Content Include="dotnetty.linux.pfx">
<pack>true</pack>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
1
2
3
4
5
6
不过在使用的时候,Nuget Package中content、contentfile目录下有相应文件,但是不会拷贝到工程目录下。
一种非主流的处理方式^_^,如果文件少可以这样处理,将文件作为嵌入资源,在对应项目中释放出来。

//在引用Nuget工程中这样导出
public Func<Stream> IndexStream { get; set; } = () => typeof(xxxnuget引用dll的某个类).GetTypeInfo().Assembly
.GetManifestResourceStream("namespace.UI.index.html");

private void InitFile()
{
using (var stream = _options.IndexStream())
{
string pathName = Path.Combine(AppContext.BaseDirectory, "index.html”);
if (!File.Exists(pathName))
{
string content = new StreamReader(stream).ReadToEnd();
StreamWriter writer = File.CreateText(pathName);
writer.WriteLine(content);
writer.Flush();
writer.Close();
}
}
}
————————————————
版权声明:本文为CSDN博主「请叫我权哥」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yiquan_yang/article/details/113517657