C#下面读写资源文件

来源:互联网 发布:随机视频聊天软件 编辑:程序博客网 时间:2024/05/16 18:44

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using  System.Resources;

namespace WindowsResource
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 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;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  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 = "写资源文件";
   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.textGreeting.Text = "textBox1";
   //
   // btnRead
   //
   this.btnRead.Location = new System.Drawing.Point(104, 200);
   this.btnRead.Name = "btnRead";
   this.btnRead.TabIndex = 2;
   this.btnRead.Text = "读资源文件";
   this.btnRead.Click += new System.EventHandler(this.btnRead_Click);
   //
   // textPassword
   //
   this.textPassword.Location = new System.Drawing.Point(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 = new 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

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [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", "Please select an item to purchase from the store:");
   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
   rr.Close();

  }
 }
 
}