Shaggydog
Shaggydog

Reputation: 3788

Embedded resource file not present at runtime .Net 4

I have a file I need to access at runtime, I've included it in my project and set it up as embedded resource (it's actually a source file, I changed the extension to .cs.txt to get around VS trying to compile it. That shouldn't matter, but I'm mentioning it anyway just in case). When I try getting the file

var assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream(resourceName);

I get a null. I've made sure I'm using the Namespace.Folder.Filename notation, but that didn't help. It appears the file is actually not there, because when I call

assembly.GetManifestResourceNames();

I get an empty string array. Any idea what could be the case?

Upvotes: 4

Views: 6584

Answers (3)

Drakkin
Drakkin

Reputation: 908

Based on this pull request (https://github.com/dotnet/msbuild/pull/5824) you can add WithCulture="false" in your csproj on your EmbeddedResource tag :

<EmbeddedResource Include="a.cs.b" WithCulture="false"/>

It is working for me

Upvotes: 1

Sam Lad
Sam Lad

Reputation: 297

I appreciate this is an old thread but what I found this morning might be useful to others.

I had a resource where the filename had multiple dots in it...

example filename: data.txt.dat

var resources = asm.GetManifestResourceNames(); // None found (empty array)

renamed to data.txt (still just an embedded resource in the project configuration

var resources = asm.GetManifestResourceNames(); // Entry found ("Assembly.Namespace.data.txt")

So maybe there is some limitation around multiple . characters in the name

Upvotes: 9

Shaggydog
Shaggydog

Reputation: 3788

So I got around this by using the VS resource manager. Now I can access the file directly like this:

MyNamespace.Properties.Resources.MyFile

I'd recommend this approach to anyone, as it seems not only much cleaner, but safer as well. Thanks Hans Passant for the advice.

Upvotes: 1

Related Questions