How to create and use resources in .NET
Asked Answered
E

4

223

How do I create a resource that I can reference and use in various parts of my program easily?

My specific problem is that I have a NotifyIcon that I want to change the icon of depending on the state of the program. A common problem, but one I've been struggling with for a long time.

Earplug answered 18/9, 2008 at 7:21 Comment(0)
E
368

Well, after searching around and cobbling together various points from around StackOverflow (gee, I love this place already), most of the problems were already past this stage. I did manage to work out an answer to my problem though.

How to create a resource:

In my case, I want to create an icon. It's a similar process, no matter what type of data you want to add as a resource though.

  • Right click the project you want to add a resource to. Do this in the Solution Explorer. Select the "Properties" option from the list.
  • Click the "Resources" tab.
  • The first button along the top of the bar will let you select the type of resource you want to add. It should start on string. We want to add an icon, so click on it and select "Icons" from the list of options.
  • Next, move to the second button, "Add Resource". You can either add a new resource, or if you already have an icon already made, you can add that too. Follow the prompts for whichever option you choose.
  • At this point, you can double click the newly added resource to edit it. Note, resources also show up in the Solution Explorer, and double clicking there is just as effective.

How to use a resource:

Great, so we have our new resource and we're itching to have those lovely changing icons... How do we do that? Well, lucky us, C# makes this exceedingly easy.

There is a static class called Properties.Resources that gives you access to all your resources, so my code ended up being as simple as:

paused = !paused;
if (paused)
    notifyIcon.Icon = Properties.Resources.RedIcon;
else
    notifyIcon.Icon = Properties.Resources.GreenIcon;

Done! Finished! Everything is simple when you know how, isn't it?

Earplug answered 18/9, 2008 at 7:21 Comment(8)
thanks for this, was looking for the exact same thing. is there a way to embed the .ico files in the executable so that they do not show as separate files? i hope my question makes sense. regards,Oxley
@Andrei: That's exactly what a resource is. Once it's added, it will be embedded as part of the compiled exe.Earplug
"...most of the problems were already past this stage" - Too True...Thanks for putting this together! I knew it was simple...in the end.Copernicus
As a side note, resource files are locked down at compile time. So, dynamically changing them at runtime does little to change the existing values. Use config files, if you wish to change values at runtime. In case this helps anyone.Bowhead
In VB.NET, resources are accessed via e.g. "My.Resources.GreenIcon"Pygidium
If you wish to divide your resources among multiple resource files (e.g., Properties.Messages, Properties.Icons, etc.), see this question and its answers.Friend
There is nowhere Properties class??Automatic
Which namespace is Properties.Resources in? I can't find this anywhere.Wat
T
10

The above didn't actually work for me as I had expected with Visual Studio 2010. It wouldn't let me access Properties.Resources, said it was inaccessible due to permission issues. I ultimately had to change the Persistence settings in the properties of the resource and then I found how to access it via the Resources.Designer.cs file, where it had an automatic getter that let me access the icon, via MyNamespace.Properties.Resources.NameFromAddingTheResource. That returns an object of type Icon, ready to just use.

Twelvemo answered 14/6, 2011 at 19:38 Comment(0)
H
8

The above method works well.

Another method (I am assuming web here) is to create your page. Add controls to the page. Then while in design mode go to: Tools > Generate Local Resource. A resource file will automatically appear in the solution with all the controls in the page mapped in the resource file.

To create resources for other languages, append the 4 character language to the end of the file name, before the extension (Account.aspx.en-US.resx, Account.aspx.es-ES.resx...etc).

To retrieve specific entries in the code-behind, simply call this method: GetLocalResourceObject([resource entry key/name]).

Hemicellulose answered 18/9, 2008 at 7:35 Comment(0)
A
3

Code posted by Matthew Scharley has a memory leak:

paused = !paused;
if (paused)
    notifyIcon.Icon = Properties.Resources.RedIcon;
else
    notifyIcon.Icon = Properties.Resources.GreenIcon;

You should Dispose() notifyIcon.Icon before replacing it, because Properties.Resources.SOME_ICON creates a new Icon each time it is used. This can be observed in the log, with this code:

Console.WriteLine(Properties.Resources.RedIcon.GetHashCode());
Console.WriteLine(Properties.Resources.RedIcon.GetHashCode());
Console.WriteLine(Properties.Resources.RedIcon.GetHashCode());

You will see 3 different Hash Codes in the log. This means these are different Objects.

So, the simple fix will be:

paused = !paused;
notifyIcon.Icon?.Dispose();
notifyIcon.Icon = paused 
                    ? Properties.Resources.RedIcon;
                    : Properties.Resources.GreenIcon;
Arbor answered 13/6, 2021 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.