#region Using directives using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Reflection; using System.Resources; using System.Windows.Forms; #endregion namespace ResourcesSample { partial class MainForm : Form { public MainForm() { InitializeComponent(); //// Load azul.jpg //this.BackgroundImage = new Bitmap(@"C:\WINDOWS\Web\Wallpaper\Azul.jpg"); //// Get this type's assembly //Assembly asm = this.GetType().Assembly; //// Enumerate the assembly's manifest resources //foreach( string resourceName in asm.GetManifestResourceNames() ) { // MessageBox.Show(resourceName); //} //// Get this type's assembly //Assembly asm = this.GetType().Assembly; //// Get the stream that holds the resource //// from the "ResourcesSample.Azul.jpg" resource //// NOTE1: Make sure not to close this stream, //// or the Bitmap object will lose access to it //// NOTE2: Also be very careful to match the case //// on the resource name itself ////Stream stream = asm.GetManifestResourceStream("ResourcesSample.Azul.jpg"); //Stream stream = asm.GetManifestResourceStream("ResourcesSample.foo.bar.Azul.jpg"); //// Load the bitmap from the stream //this.BackgroundImage = new Bitmap(stream); //// Get this type's assembly //Assembly asm = this.GetType().Assembly; //// Load the stream for resource "ResourcesSample.Azul.jpg" //Stream stream = asm.GetManifestResourceStream(this.GetType(), "Azul.jpg"); //this.BackgroundImage = new Bitmap(stream); //// Load image //this.BackgroundImage = new Bitmap(this.GetType(), "foo.bar.Azul.jpg"); this.BackgroundImage = new Bitmap(this.GetType(), "Azul.jpg"); //// Load "ResourcesSample.resources" //// from the ResourcesSampleForm class's assembly //ResourceManager resman = new ResourceManager(this.GetType()); //// Access the MyString resource string located in this form's .resx, //// using the ResourceManager //// (both of these techniques are equivalent for strings) //string s1 = (string)resman.GetObject("MyString"); //string s2 = resman.GetString("MyString"); //MessageBox.Show(s1); //MessageBox.Show(s2); // Access strongly-typed resources from Properties\Resources.resx string myString = MyResources.MyString; Icon myIcon = MyResources.MyIcon; Image myImage = MyResources.MyImage; UnmanagedMemoryStream mySound = MyResources.MySound; string myTextFile = MyResources.MyTextFile; } string resPath = @"..\..\MyResources.resx"; private void resxReaderEnumerationButton_Click(object sender, EventArgs e) { // If you use this technique, you are using the raw data and, as such, // you need to have the physical files in the appropriate directory using( ResXResourceReader reader = new ResXResourceReader(resPath) ) { foreach( DictionaryEntry entry in reader ) { string s = string.Format("{0} ({1})= '{2}'", entry.Key, entry.Value.GetType(), entry.Value); MessageBox.Show(s); } } } private void resxReaderGetValueButton_Click(object sender, EventArgs e) { using( ResXResourceReader reader = new ResXResourceReader(resPath) ) { foreach( DictionaryEntry entry in reader ) { if( entry.Key.ToString() == "MyString" ) { // Display string resource value and stop searching further MessageBox.Show("MyString = " + (string)entry.Value); break; } } } } private void resreaderEnumerationButton_Click(object sender, EventArgs e) { using( ResourceReader reader = new ResourceReader(@"..\..\MyResources.resources") ) { foreach( DictionaryEntry entry in reader ) { string s = string.Format("{0} ({1})= '{2}'", entry.Key, entry.Value.GetType(), entry.Value); MessageBox.Show(s); } } } private void resreaderGetValueButton_Click(object sender, EventArgs e) { Assembly asm = Assembly.GetExecutingAssembly(); // Load embedded .resources file Stream stream = asm.GetManifestResourceStream(this.GetType(), "MyResources.resources"); // Find resource in .resources file using( ResourceReader reader = new ResourceReader(stream) ) { foreach( DictionaryEntry entry in reader ) { if( entry.Key.ToString() == "MyString" ) { // Display string resource value and stop searching further MessageBox.Show("MyString = " + (string)entry.Value); break; } } } } private void resmanInitializeButton_Click(object sender, EventArgs e) { // Get this type's assembly Assembly asm = this.GetType().Assembly; // Load the .resources file into the ResourceManager // Assumes a project file named "Properties/Resources.resx" ResourceManager resman = new ResourceManager("ResourcesSample.MyResources", asm); } private void resmanGetValueButton_Click(object sender, EventArgs e) { Assembly asm = Assembly.GetExecutingAssembly(); ResourceManager resman = new ResourceManager("ResourcesSample.MyResources", asm); MessageBox.Show(resman.GetString("MyString")); } } }