Example explains the creation and use of resource files in NET

  • 2020-05-16 06:42:32
  • OfStack

1. Resource files
Resource files, as the name implies, are files that hold resources. Resource files have their own unique advantages in programming, they are independent of the source program, so that the resource files can be used by multiple programs. At the same time, in the process of programming, sometimes for the sake of security or other factors, the important things stored in the resource files, can also achieve the effect of confidentiality and security. So what exactly is in the resource file that Visual C# USES? Creating a resource file using Visual C# can hold roughly three types of data resources: byte arrays, various objects, and strings. This article combines a program example to illustrate how to create a resource file using Visual C#.
2. Create the class for the resource file
One of the names in.Net FrameWork SDK is the System.Resources namespace, which provides many classes and interfaces for applications to create, store, and consume resource files. There is one class called ResourceWriter, which is called Visual C# to create and store resource files.
3. How to create a resource file
You first inherit from an ResourceWriter class, then call one of the methods of the ResourceWriter class, Generate (), to generate a resource file. The specific statements are as follows:
ResourceWriter rw = new ResourceWriter ( "My.resources" ) ;
rw.Generate ( ) ;
At this point, a resource file named "My.resources" will be generated in the disk, but at this point, the resource file has no content. Let's take a look at how to add resources to the resource file.
4. How to add a resource to a resource file
An AddResource () method is provided in the ResourceWriter class to add resources to the resource file. Different resources are added in different ways in Visual C#.
(1). Add byte array, and the syntax format is:
public void AddResource ( string , byte [ ] ) ;
Note: where string is the 1-only identifier of this byte array in the program when the resource file is used
(2). Add the object, and the syntax format is:
public void AddResource ( string , object );
Note: where string is the 1-only identifier of this object in the program when the resource file is used. Such as:
 
Image image1 = Image.FromFile ("abc1.jpg") ; 
Image image2 = Image.FromFile ( "abc2.jpg" ) ; 
rw.AddResource ( "abc1" , image1 ) ; 
rw.AddResource ( "abc2" , image2 ) ; 

(3). Add a string, the specific syntax is as follows:
public void AddResource ( string1 , string2) ;
Note: where string1 is used when using a resource file, the only identifier of this string in the program is the following:
rw.AddResource ("MyStr", "read string from resource file!") );
So far, we have created a resource file and added several resources to the resource file. Of course, after this, we should also pay attention to saving this resource file and closing the resource file, as follows:
rw.Close ( ) ;
5. Create a resource file as an example
What kind of project should I create here? Some friends may use.net to create a "console application", but there is no need to use notepad to create an CS file, if the name is: CreatResources.cs. Compile command: csc CreatResources.cs; Run: CreatResources. A resource file called My.resources is generated. I'll put it here, and I'll use it later.
 
using System ; 
using System.Drawing ; 
using System.Resources ; 
class CreatResource 
{ 
public static void Main ( ) 
{ 
ResourceWriter rw = new ResourceWriter ( "My.resources" ) ; 
Image image1 = Image.FromFile ("abc1.jpg") ; 
Image image2 = Image.FromFile ( "abc2.jpg" ) ; 
rw.AddResource ( "abc1" , image1 ) ; 
rw.AddResource ( "abc2" , image2 ) ; 
Icon ic = new Icon("CDDRIVE.ICO"); 
rw.AddResource( "abc3",ic); 
rw.AddResource( "abc4"," This is the character read from the resource file "); 
rw.Generate ( ) ; 
rw.Close ( ) ; 
} 
} 

6. Add the generated resource file to the method in the test project
Create a "Windows application" test project and add the My.resources resource file to the project as follows:
1. Select the file in explorer
2. Hold down the left mouse button, drag it onto the project file, and release the left mouse button.
3. Right click on the drag-and-drop file and select "properties"
4. Select "embedded resource" in the build operation.
7. How do I manage resources in a resource file in a program
In.Net FrameWork SDK this provides a namespace for creating and using resource files -- System.Resources. There is one Class for ResourceManager in this namespace, and the main role of Class is to manage and use resource files. Visual C # is the class that manages and USES the resources in the resource files in the embedded program. The following code defines an ResourceManager class to manage the resources in the embedded application resource file:
ResourceManager rm = new ResourceManager ( " Res.My " , Assembly.GetExecutingAssembly ( ) ) ;
Note: ResourceManager rm = new ResourceManager (" Res.My ", Assembly.GetExecutingAssembly ()); In the statement, the first parameter of the constructor, Res.My, consists of two parts. Res represents the namespace of the test project, and My represents the root name of the resource file name, My.resources.
8. How do I use resources in a resource file in a program
The AddResource () method is used to add resources. The first parameter in its syntax is the user-defined string, which is the unique 1 identifier of the resource in the resource file. In programming, it is through this unique 1 identifier that a resource is used. So how do you get the required resources from this identifier in your program? This USES the GetObject () and GetString () methods of the ResourceManager class. The purpose of these two methods is to obtain the specified resource. Here is the syntax for these two methods:
object GetSting (String)
object GetObject (String)
Where "String" is the 1-only identifier of the resource in the resource file. Careful readers may have noticed that the return value of both methods is a variable of type Object, that is, a variable of type reference, while the string or image in the program is a variable of type real value. This requires a transformation, which is referred to as boxing and unboxing. The following code extracts the string, image, and icon resources from the resource file:
Extract string resources:
String s = ( ( String ) rm.GetString ( "MyStr" ) ) ;
Extract icon resources:
Icon icoDemo = ( ( Icon ) rm.GetObject ( "demo.ico" ) ) ;
Extract image resources:
Image a = ( ( Image ) ( rm.GetObject ( "ok-off.png" ) ) ) ;
9. Test project source code
 
using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
//  Add the following two namespaces.  
using System.Resources; 
using System.Reflection; 
namespace Res 
{ 
/// 
/// Form1  A summary of.  
/// 
public class Form1 : System.Windows.Forms.Form 
{ 
private System.Windows.Forms.PictureBox pictureBox1; 
private System.Windows.Forms.Button button1; 
private System.Windows.Forms.Button button2; 
private System.Windows.Forms.Button button3; 
private System.Windows.Forms.Label label1; 
private System.Windows.Forms.Button button4; 
/// 
///  Required designer variables.  
/// 
private System.ComponentModel.Container components = null; 
public Form1() 
{ 
// 
// Windows  Form designer support is required  
// 
InitializeComponent(); 
// 
// TODO:  in  InitializeComponent  Add any constructor code after the call  
// 
} 
/// 
///  Clean up all resources in use.  
/// 
protected override void Dispose( bool disposing ) 
{ 
if( disposing ) 
{ 
if (components != null) 
{ 
components.Dispose(); 
} 
} 
base.Dispose( disposing ); 
} 
#region Windows  Form designer generated code  
/// 
///  The designer supports the required methods  -  Do not use the code editor to modify  
///  The contents of this method.  
/// 
private void InitializeComponent() 
{ 
this.pictureBox1 = new System.Windows.Forms.PictureBox(); 
this.button1 = new System.Windows.Forms.Button(); 
this.button2 = new System.Windows.Forms.Button(); 
this.button3 = new System.Windows.Forms.Button(); 
this.label1 = new System.Windows.Forms.Label(); 
this.button4 = new System.Windows.Forms.Button(); 
this.SuspendLayout(); 
// 
// pictureBox1 
// 
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 
this.pictureBox1.Location = new System.Drawing.Point(8, 8); 
this.pictureBox1.Name = "pictureBox1"; 
this.pictureBox1.Size = new System.Drawing.Size(256, 240); 
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; 
this.pictureBox1.TabIndex = 0; 
this.pictureBox1.TabStop = false; 
// 
// button1 
// 
this.button1.Location = new System.Drawing.Point(280, 8); 
this.button1.Name = "button1"; 
this.button1.Size = new System.Drawing.Size(88, 24); 
this.button1.TabIndex = 1; 
this.button1.Text = " image 1"; 
this.button1.Click += new System.EventHandler(this.button1_Click); 
// 
// button2 
// 
this.button2.Location = new System.Drawing.Point(280, 48); 
this.button2.Name = "button2"; 
this.button2.Size = new System.Drawing.Size(88, 24); 
this.button2.TabIndex = 2; 
this.button2.Text = " image 2"; 
this.button2.Click += new System.EventHandler(this.button2_Click); 
// 
// button3 
// 
this.button3.Location = new System.Drawing.Point(280, 128); 
this.button3.Name = "button3"; 
this.button3.Size = new System.Drawing.Size(88, 24); 
this.button3.TabIndex = 2; 
this.button3.Text = " The text "; 
this.button3.Click += new System.EventHandler(this.button3_Click); 
// 
// label1 
// 
this.label1.Location = new System.Drawing.Point(8, 264); 
this.label1.Name = "label1"; 
this.label1.Size = new System.Drawing.Size(360, 16); 
this.label1.TabIndex = 4; 
this.label1.Text = "label1"; 
// 
// button4 
// 
this.button4.Location = new System.Drawing.Point(280, 88); 
this.button4.Name = "button4"; 
this.button4.Size = new System.Drawing.Size(88, 24); 
this.button4.TabIndex = 2; 
this.button4.Text = " icon "; 
this.button4.Click += new System.EventHandler(this.button4_Click); 
// 
// Form1 
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 
this.ClientSize = new System.Drawing.Size(376, 285); 
this.Controls.Add(this.label1); 
this.Controls.Add(this.button2); 
this.Controls.Add(this.button1); 
this.Controls.Add(this.pictureBox1); 
this.Controls.Add(this.button3); 
this.Controls.Add(this.button4); 
this.Name = "Form1"; 
this.Text = "Form1"; 
this.ResumeLayout(false); 
} 
#endregion 
/// 
///  The application's main entry point.  
/// 
[STAThread] 
static void Main() 
{ 
Application.Run(new Form1()); 
} 
private void button1_Click(object sender, System.EventArgs e) 
{ 
System.Resources.ResourceManager rm = new ResourceManager("Res.My",Assembly.GetExecutingAssembly()); 
this.pictureBox1.Image = (Bitmap)rm.GetObject("abc1"); 
} 
private void button2_Click(object sender, System.EventArgs e) 
{ 
System.Resources.ResourceManager rm = new ResourceManager("Res.My",Assembly.GetExecutingAssembly()); 
this.pictureBox1.Image = (Bitmap)rm.GetObject("abc2"); 
} 
private void button4_Click(object sender, System.EventArgs e) 
{ 
System.Resources.ResourceManager rm = new ResourceManager("Res.My",Assembly.GetExecutingAssembly()); 
this.Icon = (Icon)rm.GetObject("abc3"); 
} 
private void button3_Click(object sender, System.EventArgs e) 
{ 
System.Resources.ResourceManager rm = new ResourceManager("Res.My",Assembly.GetExecutingAssembly()); 
this.label1.Text = rm.GetString("abc4"); 
} 
} 
} 

Related articles: