NDepend Blog

Improve your .NET code quality with NDepend

How to Logically Name Embedded Resources in .csproj?

June 16, 2021 1 minutes read

You can work with .NET for two decades and still discover some useful stuff.  One thing that bothered me till now is that an embedded resource name is “the project name” .“the path to the resource where each directory separator character is changed to a dot”. This is illustrated by the screenshot below:

Embedded-Resources-Naming

The code to access the embedded resource at runtime looks like:

This is not nice because…

  • if the path of the resource gets changed,
  • or if the assembly name gets changed,
  • or if the resource gets moved to another project

…in all these situations the resource name must be kept in-sync, else a nasty bug appear! However any developer task that requires something to be kept in-sync manually in more than one location is a highly error-prone task and necessarily end up badly.

Thanks to this stackoverflow answer I discovered that the XML element <LogicalName> can be used in the .csproj file to provide a unique name to the resource. Unfortunately in the resource Properties panel this logical name is not proposed (as shown by the screenshot above). Thus I wanted to share with the world this hidden-tip that works with all flavours of .NET (.NET Fx, .NET Standard; .NET Core, .NET 5/6…)  🙂

Embedded-Resources-Naming-Logical-Name