resgen [resource filename.resx] resgen BinaryResourcesForm.resx
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Resources;
using System.Collections;
namespace BinaryResourcesApplication {
public class BinaryResourcesForm: Form {
// Constructor
public BinaryResourcesForm() {
this.SuspendLayout();
// Load the resources from the resource file
// BinaryResources.resources assuming this file
// located at the same folder as the application
// executable
using (ResourceReader reader =
new ResourceReader(@"BinaryResources.resources")) {
// Enumerate with a Dictionary object
foreach (DictionaryEntry entry in reader) {
string temp = string.Format("
Name: {0}, // Resource Name (name attribute)
Value: {1}, // Resoutce Value (value attribute)
Type: {2}", // Resource Type (type attribute)
entry.Key,
entry.Value,
entry.Value.GetType());
MessageBox.Show(temp);
}
}
this.ResumeLayout(false);
}
public static void Main() {
Application.Run(new BinaryResourcesForm());
}
}
}
csc [source file name.cs] /resource:[resource file name] csc BinaryResourcesApplication.cs /resource:BinaryResources.resources
Above example generated by running ildasm:
ildasm BinaryResourcesApplication.exe
…
.mresource public BinaryResources.resources
{
// Offset: 0x00000000 Length: 0x00002683
}
.mresource public ufo.gif
{
// Offset: 0x00002688 Length: 0x00000A01
}
…
using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Resources;
using System.Collections;
using System.Reflection;
namespace BinaryResourcesApplication {
public class BinaryResourcesForm: Form {
private Assembly assembly;
// Constructor
public BinaryResourcesForm() {
this.assembly = this.GetType().Assembly;
this.SuspendLayout();
// Load stream from the Named Container (binary resources)
Stream stream =
this.assembly.GetManifestResourceStream("BinaryResources.resources");
// Pass the stream object to the ResourceReader Constructor
using (ResourceReader reader = new ResourceReader(stream)) {
// Enumerate the resources container with a Dictionary object
foreach (DictionaryEntry entry in reader) {
// Matching entry for Form Caption
if (entry.Key.ToString() == "FormCaption") {
this.Text = (string) entry.Value;
}
}
}
// Close Stream object
stream.Close();
this.ResumeLayout(false);
}
public static void Main() {
Application.Run(new BinaryResourcesForm());
}
}
}
No comments:
Post a Comment