本分步指南引见怎样运用 C# 来作为顺序集的一部分嵌入资本,然后在运转时接见资本。
概述
.NET Framework 能够封装文件,为已编译的顺序集的一部分。这些文件称为嵌入的资本。这些资本是完全自力的顺序集相干联的.resources 和.resx 文件。您能够在运转时经由过程的System.Reflection定名空间的顺序集类接见这些资本。
嵌入的清单资本的重要长处是因为这些文件是已编译的顺序集的一部分,用户不能不测删除或误放到您的应用顺序,这在某些状况下可能会阻挠顺序的实行至关重要的文件。这类要领的一个限定是您没法保留任何变动到此文件的顺序集无需从新编译该顺序。正因为如此,只包含作为嵌入资本的应用顺序的生存期内将不会变动的文件。
分步演示
若要将嵌入的资本增加到您的项目中,必需首先为您的项目的一部分增加这些文件。将文件增加到项目后,您能够接见和显现经由过程System.Reflection定名空间中的资本。
增加嵌入的资本
要增加文本文件和图象文件作为资本嵌入到您的项目,请按照以下步骤操纵:
为此演示建立一个新的 Windows 应用顺序项目。此窗体用于显现从实行的顺序集在运转时接见的资本。
用鼠标右键单击项目称号,单击增加,然后单击增加新项
在新建项目对话框中,从菜单上,挑选文本文件,并定名为 MyTextFile.txt 的文件。在集成的开辟环境 (IDE) 中翻开了该文件,增加一些文本,,然后封闭该文件。
反复步骤 1 和 2,将位图图象增加到项目中,但而不是作为新的项目范例中挑选文本文件,挑选位图文件,然后将文件名变动为MyImage.bmp。当在 IDE 中翻开新图象时,图象上, 绘制的内容,然后封闭该文件。
用鼠标右键单击文件文本或位图,然后挑选属性
在属性对话框中,找到生成操纵属性。默许状况下,此属性设置为内容。请单击该属性并将生成操纵属性变动为嵌入的资本
对另一个文件反复步骤 4 和 5。
下一次生成项目时,编译器将这些文件增加到您的顺序集。它包含在项目中时,编译器会将项目的根定名空间增加到的资本的称号。比方,假如您的项目的根定名空间是 MyNamespace,资本被定名为 MyNamespace.MyTextFile.txt 和 MyNamespace.MyImage.bmp。
请注重: 资本文件的称号是辨别大小写。在接见资本时,您必需运用的确实拼写和大小写的文件名。假如您不运用的确实拼写和大小写的文件名,该要领挪用来接见ManifestResourceStream返回实行任何操纵,而且体系不会激发非常。
注重: 假如您想要考证这些资本称号,您能够运用 Microsoft 中心言语拆装器 (ILDASM) 检察清单数据,个中列出了所包含的资本。
接见资本
若要接见已在您的顺序集的清单中嵌入的资本,导System.IO和System.Reflection定名空间中,按以下所述:
using System.IO; using System.Reflection;
System.IO定名空间供应了流的定义和System.Reflection定名空间中定义的顺序集供应的类的要领,以接见顺序集合嵌入的资本。
声明在以下通例声明区域中的时,在加载窗体时读取从顺序集合的资本:
Assembly _assembly; Stream _imageStream; StreamReader _textStreamReader;
注重: 要接见在代码编辑器中的窗体的Load事宜,请双击该窗体在设想编辑器。
若要读取从正在实行的当前代码的顺序集的资本,您必需猎取该顺序集的一个实例。若要实行此操纵,运用该顺序集,GetExecutingAssembly要领,以下所示:
_assembly = Assembly.GetExecutingAssembly();
从资本到流中读取信息,对GetManifestResourceStream的要领挪用实行。传递给此要领的参数是要接见的资本的称号。在实行该窗体的Load事宜,然后读取两个资本到其响应的流。
_imageStream = _assembly.GetManifestResourceStream("MyNameSpace.MyImage.bmp"); _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNameSpace.MyTextFile.txt"));
在窗体的Load事宜中的代码以下所示:
try { _assembly = Assembly.GetExecutingAssembly(); _imageStream = _assembly.GetManifestResourceStream("MyNamespace.MyImage.bmp"); _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNamespace.MyTextFile.txt")); } catch { MessageBox.Show("Error accessing resources!"); }
Try-catch语句中,称为在.NET 中,结构化的毛病处理用于捕获的顺序集类的实例接见资本时可能发生的任何毛病。
显现资本
此示例运用两个按钮以显现嵌入的资本。单击第一个按钮时,基于从顺序集合读取资本的位图图象建立,并显现在窗体的图片框控件中。第二个按钮的文本资本从读取,并在文本框中显现的文本。
若要显现嵌入的资本,请实行以下步骤:
将图片框控件增加到窗体中。
将一个新的按钮控件增加到窗体中,然后再将它的Text属性变动为显现图象
双击该按钮以翻开其Click事宜,在代码检察器中,然后将下面的代码粘贴在这类状况:
try { pictureBox1.Image = new Bitmap(_imageStream); } catch { MessageBox.Show("Error creating image!"); }
该代码生成基于窗体的Load事宜中读取资本流的位图中的一个新实例。
向表单中增加文本框控件。
将另一个按钮控件增加到窗体上,然后再将它的Text属性变动为猎取文本
双击设想编辑器中翻开Click_Event的按钮,该按钮,然后将下面的代码粘贴在该事宜中:
try { if(_textStreamReader.Peek() != -1) { textBox1.Text = _textStreamReader.ReadLine(); } } catch { MessageBox.Show("Error writing text!"); }
此代码确定要读取的字符是不是依然存在在流中。假如找到的字符,文本框中将读取行。
按 F5 键运转该应用顺序。
完全的代码
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Reflection; namespace MyNamespace { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support. // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call. // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // pictureBox1 // this.pictureBox1.Location = new System.Drawing.Point(4, 8); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(284, 192); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(92, 236); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(192, 20); this.textBox1.TabIndex = 1; this.textBox1.Text = "textBox1"; // // button1 // this.button1.Location = new System.Drawing.Point(8, 208); this.button1.Name = "button1"; this.button1.TabIndex = 2; this.button1.Text = "Show Image"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(8, 236); this.button2.Name = "button2"; this.button2.TabIndex = 3; this.button2.Text = "Get Text"; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.AddRange(new System.Windows.Forms.Control[]{ this.button2, this.button1, this.textBox1, this.pictureBox1}); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion Assembly _assembly; Stream _imageStream; StreamReader _textStreamReader; /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { try { _assembly = Assembly.GetExecutingAssembly(); _imageStream = _assembly.GetManifestResourceStream("MyNamespace.MyImage.bmp"); _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNamespace.MyTextFile.txt")); } catch { MessageBox.Show("Error accessing resources!"); } } private void button1_Click(object sender, System.EventArgs e) { try { pictureBox1.Image = new Bitmap(_imageStream); } catch { MessageBox.Show("Error creating image!"); } } private void button2_Click(object sender, System.EventArgs e) { try { if(_textStreamReader.Peek() != -1) { textBox1.Text = _textStreamReader.ReadLine(); } } catch { MessageBox.Show("Error writing text!"); } } } }
注重在 Visual Studio 2005年中或在 Visual Studio 2008 中,则应变动代码。建立一个 Windows 窗体项目时,Visual C# 一个窗体向项目中增加默许状况下。此窗体名为 Form1。示意窗体的两个文件称为 Form1.cs 和 Form1.designer.cs。Form1.cs 中编写您的代码。Designer.cs 文件是 Windows 窗体设想器编写的代码完成的一切操纵,您经由过程增加控件来实行。有关 Windows 窗体设想器在 Visual C# 2005年或 Visual Studio 2008 中的细致信息,请接见下面的 Microsoft Web 站点:
http://msdn2.microsoft.com/en-us/library/ms173077.aspx
毛病消除
因为资本称号是辨别大小写,请考证您正在运用响应的拼写和大小写的资本的接见。您能够运用 ILDASM 读取清单的数据,来考证资本的确实拼写。
以上就是怎样嵌入和运用C#来接见资本的代码详解的细致内容,更多请关注ki4网别的相干文章!