📄 mainform.cs
字号:
#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 "ResourcesSample.resources"
// from the MainForm class's assembly
ResourceManager resman = new ResourceManager(this.GetType());
// Access the MyString string from 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);
}
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(@"C:\Documents and Settings\mikeDub\Desktop\ResourcesSample\Properties\Resources.resx") ) {
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(@"C:\Documents and Settings\mikeDub\Desktop\ResourcesSample\Properties\Resources.resx") ) {
foreach( DictionaryEntry entry in reader ) {
if( entry.Key.ToString() == "MyString" ) {
// Display string resource value
MessageBox.Show("MyString = " + (string)entry.Value);
}
}
}
}
private void resreaderEnumerationButton_Click(object sender, EventArgs e) {
using( ResourceReader reader = new ResourceReader(@"ResourcesSample.Properties.Resources.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 assem = Assembly.GetExecutingAssembly();
// Load embedded .resources file
using( Stream stream = assem.GetManifestResourceStream(this.GetType(), "Properties.Resources.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
MessageBox.Show("MyString = " + (string)entry.Value);
}
}
}
}
}
private void resmanInitializeButton_Click(object sender, EventArgs e) {
// Get this type's assembly
Assembly assem = this.GetType().Assembly;
// Load the .resources file into the ResourceManager
// Assumes a project file named "Properties/Resources.resx"
ResourceManager resman = new ResourceManager("ResourcesSample.Properties.Resources", assem);
}
private void resmanGetValueButton_Click(object sender, EventArgs e) {
Assembly assem = Assembly.GetExecutingAssembly();
ResourceManager resman = new ResourceManager("ResourcesSample.MyResourceFile", assem);
MessageBox.Show(resman.GetString("MyString"));
}
private void stronglyTypedResourceGetValueButton_Click(object sender, EventArgs e) {
MessageBox.Show(ResourcesSample.Properties.Resources.MyString);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -