C # Read and write resource files below

xiaoxiao2021-03-05  22

Using system.drawing; using system.collections; using system.windows.form; using system.data; using system.resources;

A summary description of Namespace WindowsResource {///

/// form1. /// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox textGreeting; private System.Windows.Forms.TextBox textPassword; private System.Windows.Forms.TextBox textPurchase; private System.Windows.Forms.Button BtnWrite; private System.Windows.Forms.Button btnRead; private System.Windows.Forms.Button Button1; /// /// The required designer variable. /// private system.componentmodel.container components = null;

Public Form1 () {// // Windows Form Designer Support for // InitializeComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.BtnWrite = new System.Windows.Forms.Button (); this.textGreeting = new System.Windows.Forms.TextBox (); this.btnRead = new System. Windows.Forms.Button (); this.textPassword = new System.Windows.Forms.TextBox (); this.textPurchase = new System.Windows.Forms.TextBox (); this.label1 = new System.Windows.Forms.Label (); This.Label2 = new system.windows.forms.label (); this.label3 = new system.windows.forms.label (); this.button1 = new system.windows.Forms.Button (); this. SuspendLayout (); // // btnwrite // this.btnwrite.location = new system.drawing.point (24, 200); this.btnwrite.name = "btnwrite"; this.btnwrite.tabindex = 0; this.btnwrite .Text = "Write resource file"; this.btnwrite.click = new system.eventhandler (this.btnwrite_click; /// textgreeting // this.textgreeting.location = new system.drawing.point (272, 48) This.TextGreeting.Name = "textgreeting"; this.textgreeting.tabindex = 1; this.textg Reeting.text = "textbox1"; // // btnread // this.btnread.location = new system.drawing.point (104, 200); this.btnread.name = "btnread"; this.btnread.tabindex = 2 "This.btnread.text =" Read the resource file "; this.btnread.click = new system.eventhandler (this.btnread_click); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////> 272, 88); this.TextPassword.name = "textpassword"; this.textpassword.tabindex = 3; this.textpassword.text = "textbox2"; // // textpurchase // this.textpurchase.location =

New system.drawing.point (272, 136); this.textpurchase.name = "textpurchase"; this.textpurchase.tabindex = 4; this.textpurchase.text = "textbox3"; // // label1 // this.label1 .Location = new system.drawing.point (154, 56); this.label1.name = "label1"; this.label1.size = new system.drawing.size (100, 18); this.label1.tabindex = 5 This.Label1.Text = "greeting"; // // label2 // this.label2.location = new system.drawing.point (154, 104); this.label2.name = "label2"; this.label2. Size = new system.drawing.size (100, 18); this.label2.tabindex = 6; this.label2.text = "passwordException"; // // label3 // this.label3.location = new system.drawing. Point (154, 144); this.label3.name = "label3"; this.label3.size = new system.drawing.size (100, 18); this.label3.tabindex = 7; this.label3.text = " Purchase "; // // Button1 // this.button1.location = new system.drawing.point (192, 200); this.button1.name =" button1 "; this.button1.size = ne w system.drawing.size (88, 23); this.button1.tabindex = 8; this.button1.text = "resourceReader"; this.button1.click = new system.eventhandler (this.button1_click); // / / Form1 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (400, 293); this.Controls.add (this.button1); this.Controls .Add (this.label3); this.controls.add (this.label2); this.controls.add (this.label1); this.controls.add (this.TextPurchase); this.Controls.add (this.TextPassword This.Controls.add (this.btnread);

This.Controls.add (this.TextGreeting); this.controls.add (this.btnwrite); this.name = "form1"; this.text = "form1"; this.ResumeLayout (false);} #ENDREGION

///

/// The primary entry point of the application. /// [stathread] static void main () {Application.run (new form1 ());}

Private void btnwrite_click (object sender, system.eventargs e) {resourceWriter RW = New ResourceWriter ("Greeting.Resources");

Bitmap B = New Bitmap ("HHW.GIF");

// Add Some strings to the file rw.addresource ("Greeting", "Welcome to Microsoft .NET Framework! Test"); rw.addresource ("PasswordException", "Sorry, That is not the correct password."); RW .AddResource ("purchase", "plechase"; rw.addresource ("Goodbye", "Thank you for visiting microsoft .net framework!"); Rw.addResource ("flag", b);

Rw.Generate (); rw.close ();

private void btnRead_Click (object sender, System.EventArgs e) {ResourceManager rm = ResourceManager.CreateFileBasedResourceManager ( "Greeting",, null "."); textGreeting.Text = rm.GetString ( "Greeting"); textPassword.Text = rm. GetString ("PasswordException"); TextPurchase.text = rm.getstring ("purchase"); RM.GetString ("Goodbye"); Bitmap B = (Bitmap) RM.GetObject ("flag"); this.backgroundimage = B;

}

private void button1_Click (object sender, System.EventArgs e) {ResourceReader rr = new ResourceReader ( "Greeting.resources"); String s = ""; // iterate through the reader, printing out the name-value pairs foreach (DictionaryEntry d IN rr) {s = "text" d.Key; if (s == "textgreeting") {textgreeting.text = D.Value.toString ();} if (s == "textpassword") {textPassword.text = D.Value.toString ();} if (s == "textpurchase") {textpurchase.text = D.Value.toString ();}} // close the reader r.close ();}}

转载请注明原文地址:https://www.9cbs.com/read-33157.html

New Post(0)