Visual Studio: cannot access resources from code
Solution 1
Make the file as a resource from its properties as:
BuildAction=Compile
Solution 2
You need to add them here:
- Project menu
- MyProjectName Properties
- Resources tab
- "Click here to create resources" (if necessary)
- Select Files menu
- 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.
alapeno
Updated on June 09, 2021Comments
-
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 about 12 yearsthe 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 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 about 12 yearsThanks 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 about 12 yearsOh, I first thought you mean the specific resource files. Did you mean the resource.designer.cs instead? This is the point, thanks!
-
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
orexe
. -
micahhoover about 11 yearsSolved 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.