如何防止根据其文件名设置嵌入式资源文件的区域性 [英] How can I prevent embedded resource file culture being set based on its filename

查看:62
本文介绍了如何防止根据其文件名设置嵌入式资源文件的区域性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VS2015 .NET 4.6.1项目中,我有一个数据资源文件,其构建操作"设置为嵌入式资源",其名称如下:

In a VS2015 .NET 4.6.1 project, I have a data resource file with a Build Action set to 'Embedded Resource' and a name as follows:

myDataFile.bin.zip

不幸的是,"bin"是我的Windows 10计算机上存在的一种语言环境,这导致该文件被标识为特定语言环境的资源,而不是默认语言环境(Visual Studio似乎可以识别文件中命名的文件).格式为<name>.<locale>.<ext>(特定于语言环境),从而导致创建附属程序集.

Unfortunately 'bin' is a locale that is present on my Windows 10 machine, which is causing the file to be identified as a resource for a specific locale, rather than the default one (Visual Studio seems to recognise files named in the format <name>.<locale>.<ext> as being specific to a locale), resulting in the creation of a satellite assembly.

这意味着在构建时,我得到了两个程序集:

This means that when built, I get two assemblies:

myproject.dll
bin\myproject.resources.dll

我希望文件包含在项目程序集中,而不是附属程序中.

I want the file to be included in the project assembly, not in a satellite assembly.

不更改文件名,这可能吗?

Without changing the filename, is this possible?

谢谢!

推荐答案

此行为由Microsoft.Common.CurrentVersion.targets(位于C:\ Program Files(x86)\ MSBuild \ 14.0 \ Bin)和 Microsoft.Build.Tasks.AssignCulture 类.我查看了源代码,但没有找到可以切换的简单开关,以防止将"bin"误解为区域性名称.实际上,对于C#项目,当嵌入式资源依赖于"同名的.cs文件时,文件名仅被单独保留.

This behavior is implemented by the SplitResourcesByCulture build target in Microsoft.Common.CurrentVersion.targets (located at C:\Program Files (x86)\MSBuild\14.0\Bin) and the Microsoft.Build.Tasks.AssignCulture class. I looked at the source code, but I didn't find a simple switch you can toggle to prevent "bin" from being misinterpreted as a culture name. In fact, for a C# project, the file name is left alone only when the embedded resource is "dependent upon" a like-named .cs file.

因此,无需重写标准的构建目标,您将有一个简单的解决方法(如果有些棘手):

So, short of rewriting the standard build targets, you have a simple (if slightly hacky) workaround:

  1. 在myDataFile.bin.zip所在的文件夹中,将一个名为myDataFile.bin.cs的空C#代码文件添加到项目中. 此名称必须与嵌入式资源的名称完全相同 ,除了最后的扩展名(.cs而不是.zip).您可以将文件完全空白,也可以添加评论以说明发生了什么事情:

  1. In the folder where myDataFile.bin.zip is located, add to the project an empty C# code file named myDataFile.bin.cs. This name must match the name of your embedded resource exactly except for the final extension (.cs instead of .zip). You can leave the file totally blank, or you can add a comment explaining what's going on:

// This source file prevents the nested myDataFile.bin.zip file from being treated as
// a resource for the "bin" culture.

  • 保存项目. (为确保保存项目文件,请单击文件»全部保存.)

  • Save the project. (To ensure the project file gets saved, click File » Save All.)

    在文本编辑器中打开.csproj项目文件,然后找到myDataFile.bin.zip的<EmbeddedResource>元素:

    Open the .csproj project file in a text editor and locate the <EmbeddedResource> element for myDataFile.bin.zip:

    <EmbeddedResource Include="myDataFile.bin.zip" />
    

  • 插入引用C#代码文件的子<DependentUpon>元素:

    <EmbeddedResource Include="myDataFile.bin.zip">
      <DependentUpon>myDataFile.bin.cs</DependentUpon>
    </EmbeddedResource>
    

  • 保存项目文件,然后在Visual Studio中重新加载项目.

  • Save the project file and reload the project in Visual Studio.

    在解决方案资源管理器中直观地确认myDataFile.bin.zip嵌套在myDataFile.bin.cs下.

    Visually confirm in Solution Explorer that myDataFile.bin.zip is nested under myDataFile.bin.cs.

    构建项目.您的资源现在嵌入在主装配体中,而不是附属装配体中.

    Build the project. Your resource is now embedded in the main assembly instead of a satellite assembly.

    这篇关于如何防止根据其文件名设置嵌入式资源文件的区域性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆