.net - 如何在 Nuget 包中包含 resx 文件

标签 .net visual-studio nuget resources nuspec

我有一个 .Net Framework 4.6.1 项目,我从中制作了一个 nuget 包。这个 nuget 包只安装了 2 个 dll 和几个工作正常的内容文件。
问题是我现在通过位于以下位置的 resx 文件添加了资源字符串:

~\App_Data\Global\Resources\resource-strings.resx
而且我不知道如何使该文件正确地成为 nuget 包的一部分。当我创建 .nupkg 时,我在那里看到了 resx 文件,但是当我将它安装在另一个项目上时,resx 文件应该被复制到 App_Data\Global\Resources 文件夹中,但事实并非如此。
是否可以?
根据我的调查,我认为我还必须对目标文件 + nuspec 配置做一些事情,但我尝试过的一切都不起作用。
我真的只需要复制 resx 文件。没有比这更复杂的了。

最佳答案

当然 .这是可能的,可以通过 nuget 来完成。由于您希望将此资源文件复制到您的目标 asp net 项目文件夹中,您可以尝试以下步骤:
================================================== ===
第一 , 如果你想安装这个 net461 nuget 包成 net framework asp net项目,你应该使用content nodexxx.nusepc文件
首先,确保 构建操作 资源strings.resx 是嵌入式资源而不是 内容 .
1) 第一 , 运行 cmd 命令:cd xxxx(project folder path)然后运行 ​​nuget spec生成 nuspec 文件。这些就够了:
2) 打开 nuspec文件并添加内容节点:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>xxx</id>
        <version>xxx</version>
        <title>xxx</title>
        <authors>xxx</authors>
        ............       
    </metadata>
    <files>
        <file src="~\App_Data\Global\Resources\resource-strings.resx(the path of the file in net framework 4.6.1 project)" target="content\App_Data\Global\Resources\resource-strings.resx" />
    </files>
</package>
3) 然后保存 nuspec文件并运行 nuget pack生成 nupkg .
之前 你安装nuget包,你应该先clean nuget caches删除旧的错误版本的nuget。
当你安装这个包时,文件会被复制到根路径 App_Data\Global\Resources\resource-strings.resx的网络项目。
================================================== ==
如果您想将此包安装到新的 sdk 项目中(Net Core 或 xxx with PackageReference nuget 管理格式),您应该创建一个带有复制任务的目标文件。
1) 在net framework 4.6.1项目中添加一个名为build的文件夹,然后添加一个名为<Package_id>.props的文件文件。
备注 您应该确保 nuget 包的 ID 与 <Package_id>.props 相同. Hint from here .
2) 将这些添加到 <Package_id>.props :
  <Project>
      <Target Name="CopyFilesToProject" BeforeTargets="Build">
        <Message Text="Copy resource-strings.resx  to project" />
        <ItemGroup>
          <SourceScripts Include="$(MSBuildThisFileDirectory)..\content\**\*.*"/>
        </ItemGroup>
        <Copy
           SourceFiles="@(SourceScripts)"
           DestinationFiles="$(MSBuildProjectDirectory)\%(RecursiveDir)%(Filename)%(Extension)"/>
      </Target>
      
    </Project>
3) 修改xxx.nuspec像这样的文件:
<?xml version="1.0"?>
<package >
  <metadata>
    <id>xxx</id>
    <version>xxx</version>
    <title>xxx</title>
    <authors>xxx</authors>
    <owners>me</owners>
    ............
  </metadata>
 <files>
<file src="~\App_Data\Global\Resources\resource-strings.resx" target="content\App_Data\Global\Resources\resource-strings.resx" />
<file src="build\xxx(like package_id).props" target="build"/>
</files>

</package>
4) 那么你应该使用 nuget pack命令来打包这个项目。在安装这个包之前,你应该先清理 nuget 缓存。
你安装这个nuget包,你应该构建 您的项目运行此自定义复制目标以将文件复制到您的主项目中。
还有 ,还有a similar issue对这个。

关于.net - 如何在 Nuget 包中包含 resx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63489674/

相关文章:

.net - wpf 数据网格角度标题样式

javascript - 从全日历的月 View 中鼠标悬停时未显示工具提示?

c# - 无法删除文件,复制和移动在 C# VS2013 中不起作用

c# - NUGet 定制?

nuget - 如何获取离线安装的所有 nuget 依赖项

c# - 删除对扩展的引用

c# - 是否存在 Application.Exit() 不引发 FormClosing 事件的情况?

c# - 将 Func<T, int> 值插入 linq-2-sql 查询

.net - GAC Scope : why does it override local assemblies, 有什么我们可以做的吗?

c++ - #include <boost/chrono.hpp> 导致无法解析的外部符号,使用了 bcp