[c#] How to create and use resources in .NET

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.

This question is related to c# visual-studio resources icons

The answer is


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?


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.


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.


The above method works good.

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]).


The above method works good.

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]).


The above method works good.

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]).


The above method works good.

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]).


Questions with c# tag:

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array How can I add raw data body to an axios request? Couldn't process file resx due to its being in the Internet or Restricted zone or having the mark of the web on the file Convert string to boolean in C# Entity Framework Core: A second operation started on this context before a previous operation completed ASP.NET Core - Swashbuckle not creating swagger.json file Is ConfigurationManager.AppSettings available in .NET Core 2.0? No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization Getting value from appsettings.json in .net core .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Automatically set appsettings.json for dev and release environments in asp.net core? How to use log4net in Asp.net core 2.0 Get ConnectionString from appsettings.json instead of being hardcoded in .NET Core 2.0 App Unable to create migrations after upgrading to ASP.NET Core 2.0 Update .NET web service to use TLS 1.2 Using app.config in .Net Core How to send json data in POST request using C# ASP.NET Core form POST results in a HTTP 415 Unsupported Media Type response How to enable CORS in ASP.net Core WebAPI VS 2017 Metadata file '.dll could not be found How to set combobox default value? How to get root directory of project in asp.net core. Directory.GetCurrentDirectory() doesn't seem to work correctly on a mac ALTER TABLE DROP COLUMN failed because one or more objects access this column Error: the entity type requires a primary key How to POST using HTTPclient content type = application/x-www-form-urlencoded CORS: credentials mode is 'include' Visual Studio 2017: Display method references Where is NuGet.Config file located in Visual Studio project? Unity Scripts edited in Visual studio don't provide autocomplete How to create roles in ASP.NET Core and assign them to users? Return file in ASP.Net Core Web API ASP.NET Core return JSON with status code auto create database in Entity Framework Core Class Diagrams in VS 2017 How to read/write files in .Net Core? How to read values from the querystring with ASP.NET Core? how to set ASPNETCORE_ENVIRONMENT to be considered for publishing an asp.net core application? ASP.NET Core Get Json Array using IConfiguration Entity Framework Core add unique constraint code-first No templates in Visual Studio 2017 ps1 cannot be loaded because running scripts is disabled on this system

Questions with visual-studio tag:

VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? How to download Visual Studio Community Edition 2015 (not 2017) Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio Code pylint: Unable to import 'protorpc' Open the terminal in visual studio? Is Visual Studio Community a 30 day trial? How can I run NUnit tests in Visual Studio 2017? Visual Studio 2017: Display method references Visual Studio 2017 errors on standard headers Where is NuGet.Config file located in Visual Studio project? Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state Visual Studio 2017 - Git failed with a fatal error Unity Scripts edited in Visual studio don't provide autocomplete Equivalent to AssemblyInfo in dotnet core/csproj How to enable C++17 compiling in Visual Studio? Nuget connection attempt failed "Unable to load the service index for source" Change language of Visual Studio 2017 RC Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies Visual Studio Code how to resolve merge conflicts with git? Getting "project" nuget configuration is invalid error "Object doesn't support property or method 'find'" in IE localhost refused to connect Error in visual studio Why is the Visual Studio 2015/2017/2019 Test Runner not discovering my xUnit v2 tests Build error, This project references NuGet Visual Studio Code always asking for git credentials Stuck while installing Visual Studio 2015 (Update for Microsoft Windows (KB2999226)) Visual Studio error "Object reference not set to an instance of an object" after install of ASP.NET and Web Tools 2015 C# - How to convert string to char? Multiple Errors Installing Visual Studio 2015 Community Edition Why Visual Studio 2015 can't run exe file (ucrtbased.dll)? Where can I read the Console output in Visual Studio 2015 Go To Definition: "Cannot navigate to the symbol under the caret." CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found How to remove error about glyphicons-halflings-regular.woff2 not found NuGet Packages are missing Visual Studio Community 2015 expiration date How to solve ERR_CONNECTION_REFUSED when trying to connect to localhost running IISExpress - Error 502 (Cannot debug from Visual Studio)? Where is the visual studio HTML Designer? error C4996: 'scanf': This function or variable may be unsafe in c programming What are the differences between Visual Studio Code and Visual Studio? Is Xamarin free in Visual Studio 2015? What's the difference between Visual Studio Community and other, paid versions? Visual Studio 2015 installer hangs during install? How to use class from other files in C# with visual studio? 'cannot find or open the pdb file' Visual Studio C++ 2013 How to build x86 and/or x64 on Windows from command line with CMAKE? Python Pip install Error: Unable to find vcvarsall.bat. Tried all solutions How to enable C# 6.0 feature in Visual Studio 2013?

Questions with resources tag:

Spring Boot access static resources missing scr/main/resources How do I add a resources folder to my Java project in Eclipse Tomcat 8 throwing - org.apache.catalina.webresources.Cache.getResource Unable to add the resource Reading a resource file from within jar How to fix the "508 Resource Limit is reached" error in WordPress? How to get absolute path to file in /resources folder of your project getResourceAsStream returns null How to read file from res/raw by name Load image from resources Resource leak: 'in' is never closed What is the JSF resource library for and how should it be used? How can I get a resource "Folder" from inside my jar File? Getting android.content.res.Resources$NotFoundException: exception even when the resource is present in android Spring 3 MVC resources and tag <mvc:resources /> Correct way to quit a Qt program? How do you obtain a Drawable object from a resource id in android package? Android: How do I get string from resources using its name? REST URI convention - Singular or plural name of resource while creating it How to correctly get image from 'Resources' folder in NetBeans Android/Eclipse: how can I add an image in the res/drawable folder? Android: How to add R.raw to project? Change PictureBox's image to image from my resources? Utils to read resource text file to String (Java) How do I go about adding an image into a java project with eclipse? Any easy way to use icons from resources? How to use ClassLoader.getResources() correctly? TextView bold via xml file? Android: alternate layout xml for landscape mode How to use relative paths without including the context root name? android.view.InflateException: Binary XML file line #12: Error inflating class <unknown> Android, getting resource ID from string? Android read text raw resource file Get a list of resources from classpath directory Preferred way of loading resources in Java Android OnClickListener - identify a button Add floating point value to android resources/values Change app language programmatically in Android How to get access to raw resources that I put in res folder? Load properties file in JAR? How can I get relative path of the folders in my android project? Setting Icon for wpf application (VS 08) Get a resource using getResource() DisplayName attribute from Resources? Where to place and how to read configuration resource files in servlet based application? Accessing a resource via codebehind in WPF What does MissingManifestResourceException mean and how to fix it? How to get a path to a resource in a Java JAR file How to access static resources when mapping a global front controller servlet on /* Should a RESTful 'PUT' operation return something Java resource as file

Questions with icons tag:

How to use the new Material Design Icon themes: Outlined, Rounded, Two-Tone and Sharp? Round button with text and icon in flutter Add tooltip to font awesome icon How to add icons to React Native app How to set app icon for Electron / Atom Shell App Android Push Notifications: Icon not displaying in notification, white square shown instead Pyinstaller setting icons don't change How to change Toolbar home icon color iOS how to set app icon and launch images Put search icon near textbox using bootstrap Actionbar notification count icon (badge) like Google has How to display count of notifications in app launcher icon Set Icon Image in Java Rebuild or regenerate 'ic_launcher.png' from images in Android Studio How to add a spinner icon to button when it's in the Loading state? Can I change the color of Font Awesome's icon color? Unicode via CSS :before Where can I find Android's default icons? How to style icon color, size, and shadow of Font Awesome Icons Change default icon Any easy way to use icons from resources? What does the red exclamation point icon in Eclipse mean? Set icon for Android application How to add a browser tab icon (favicon) for a website? How do I add an image to a JButton How to make/get a multi size .ico file? Which icon sizes should my Windows application's icon include? Change icons of checked and unchecked for Checkbox for Android What size should apple-touch-icon.png be for iPad and iPhone? What characters can be used for up/down triangle (arrow without stem) for display in HTML? Setting Icon for wpf application (VS 08) iPhone App Icons - Exact Radius? How to change JFrame icon What do the icons in Eclipse mean? How do I get my page title to have an icon? TortoiseSVN icons not showing up under Windows 7 Put icon inside input element in a form How do I set the icon for my application in visual studio 2008? How do I change the default application icon in Java? How to create and use resources in .NET