4

It is always confusing for me whenever I get to the tasks of Path-Resolution. I have the following information regarding the issue reported:

  • Using ASP.NET MVC-5 Application
  • Trying to access a font file i.e. MyriadPro-SemiBold.ttf via below code
//a value receives a path + name of the file in variable i.e. name
string name = "myApplicationName.fonts.MyriadPro-Semibold.ttf";

//object (named as assembly) of class System.Reflection.Assembly. 
var assembly = Assembly.GetExecutingAssembly();

using (Stream stream = assembly.GetManifestResourceStream(name))
// stream always gets null value (don't know why!)
{
    if(stream != null)
    {
        //myCode waiting for above stream not to be null :-(
    }
    else
    {
        throw new ArgumentException("No resource with name " + name);
    }
}

I don't know much about the way Visual Studio works in different types of Applications in the aspect of paths.

halfer
  • 19,824
  • 17
  • 99
  • 186
Asif Mehmood
  • 964
  • 2
  • 14
  • 35
  • 1
    System.Reflection.Assembly.ReflectionOnlyLoadFrom(path) ? Possible duplicate of http://stackoverflow.com/questions/10726857/why-does-getmanifestresourcestream-returns-null-while-the-resource-name-exists-w – Seth Kitchen Mar 23 '16 at 17:43
  • The code line you provided gives me an Exception **{"Could not load file or assembly 'file:///C:\\Program Files.....\\myApplicationName.fonts.MyriadPro-Semibold.ttf' or one of its dependencies. The system cannot find the file specified.":"file:///C:\\Program Files....\\myApplicationName.fonts.MyriadPro-Semibold.ttf"}** – Asif Mehmood Mar 23 '16 at 17:57
  • 1
    Your path looks wrong. Get ride of. File:/// the double slashes and make sure the file name is at that directory in your file explorer. – Seth Kitchen Mar 23 '16 at 18:00
  • Yes, this was also the solution. Thank you very much @SethKitchen . The below accepted answer worked for me – Asif Mehmood Mar 23 '16 at 18:25

1 Answers1

14

Your resource should be embedded:

enter image description here

You can make a little test to get all your resources and find the good name. After that your code seems correct.

 var allRessources= System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();

 var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("fullpath here");
 if (stream == null) return;

EDIT

To add a file in VS project as embedded resource: just add the file to your project, click on it, and then under Properties set Build Action to Embedded Resource. And that's it!

More information about embedded resource: https://support.microsoft.com/en-us/kb/319292#bookmark-4

halfer
  • 19,824
  • 17
  • 99
  • 186
Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
  • allRessources gets {string[0]} while debugging. Does that mean that my resource is not embedded. (I didn't know about embedded resources, can you share some thing) – Asif Mehmood Mar 23 '16 at 17:56
  • 2
    Yes I think your ressource is not embedded. If you are using visual studio, just add the file to your project, click on it, and then under **Properties** set **Build Action** to **Embedded Resource**. And thats it! – Quentin Roger Mar 23 '16 at 17:58
  • Now the resource is being shown in **allRessources** while debugging. Thanks for this. – Asif Mehmood Mar 23 '16 at 18:12
  • I'm happy this helped – Quentin Roger Mar 23 '16 at 18:15
  • 1
    @QuentinRoger after centuries of resultless searching I finally came here and find perfect solution. An ocean of thanks to you! – mankers May 30 '17 at 11:14
  • thanks you, i need to use embeded ressources instead ressources since i have change my net framework from 4.8 to net 7 – Rachid Benbrik Jun 12 '23 at 15:22