Visual Studio: cannot access resources from code

24,945

Solution 1

Make the file as a resource from its properties as:

BuildAction=Compile

Solution 2

You need to add them here:

  1. Project menu
  2. MyProjectName Properties
  3. Resources tab
  4. "Click here to create resources" (if necessary)
  5. Select Files menu
  6. Add Resource

Now you can access them using MyProjectName.Properties.Resources.Filename

Solution 3

Take a look at this MSDN Forum link. What I take from it is make sure your resources are added as resources and that the namespace's match.

Share:
24,945
alapeno
Author by

alapeno

Updated on June 09, 2021

Comments

  • alapeno
    alapeno almost 3 years

    I added several files (HTML, CSS, etc.) as resources to my Visual Studio project. According to MSDN, I should be able to access these resources via

    MyProjectName.Properties.Resources.Filename
    

    Unfortunately, IntelliSense doesn't even know the Properties property behind MyProjectName. If I type "MyProjectName.", IntelliSense offers me several things but no properties. I already built the project, but nothing changed.

    If this is important: that project is not the main/startup project of my Visual Studio solution.

  • alapeno
    alapeno about 12 years
    the namespace of the properties file is "Projectname.Converter.Properties" and the namespace of the class in which I want to use the resource is "Projectname.Converter.XY". does that mean I only can use one single namespace in a project if I want to use resources? Cannot imagine that
  • Mark Hall
    Mark Hall about 12 years
    @alapeno The accepted answer of the MSDN Forum link seems to state that, though he had an issue with his Base Namespace being different. All I can say is try changing it to see if it is the issue. Just trying to give you some ideas
  • alapeno
    alapeno about 12 years
    Thanks Mark, I got that answer of the MSDN forum wrong. As you said, the basenamespace was the issue there, but not in my case. My Resource.Designer.cs build action was set to "embedded resource" instead of compile, maybe that was the problem, it finally works
  • alapeno
    alapeno about 12 years
    Oh, I first thought you mean the specific resource files. Did you mean the resource.designer.cs instead? This is the point, thanks!
  • Mark Hall
    Mark Hall about 12 years
    @alapeno I am glad. The embedded resource's being the issue kind of makes sense since the files are being compiled into the dll or exe.
  • micahhoover
    micahhoover about 11 years
    Solved my problem. Not allowing devs to access their resources from R-clicking on resources and adding the files is very strange to say the least and has probably caused a lot of frustration.