How to change the generated ManifestResourceName for Embedded Resources

Viewed 1113

In a C# project, the generated resource name for an Embedded Resource by default undergoes some mangling to make the resulting name a namespaced valid language identifier.

For example Foo/Bar/123.txt becomes {Default Namespace}.Foo.Bar._123.txt and I have to load it with asm.GetManifestResourceStream("Yuck.Foo.Bar._123.txt").

I would much rather have my resources retain their relative path such as Foo/Bar/123.txt to better simulate a virtual file system and so I can load them such as asm.GetManifestResourceStream("Foo/Bar/123.txt").

Now I know that .Net does not place restrictions on resource names, so it seems feasible.

But how do I get the build engine to generate sensible path names for me?

2 Answers

If you're using "new-style" (aka "SDK-style") .csproj files then you can take advantage of new behaviour in MSBuild that allows properties on MSBuild Items to be specified as attributes instead of as child elements.

So instead of doing this:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <!-- etc -- >

  <ItemGroup>
    <EmbeddedResource Include="Foo/Bar/123.txt">
      <LogicalName>FooBar123Txt</LogicalName>
    </EmbeddedResource>
  </ItemGroup>

</Project>

You can now do this:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <!-- etc -- >

  <ItemGroup>
    <EmbeddedResource Include="Foo/Bar/123.txt" LogicalName="FooBar123Txt" />
  </ItemGroup>

</Project>
Related