?? ??? ?? ??? ???? ????? ???? ?? ?? ???? ????? ???? ?? ??? ????? C# ??????? ?? ?? ??? ?? ??? ??? ?????? ?????? ????, ?? ???? ??? ????? ??? ???????? ?? ??????? ?? ????
????????? ??????
.NET Framework ??????? ?? ???? ?????? ??????? ?? ?? ??? ?? ??? ??? encapsulate ?? ???? ???? ?? ??????? ?? ???????? ???????? ?? ??? ??? ???? ???? ???? ?? ???????? .resources ?? .resx ??????? ?? ??????? ?? ?????? ??? ?? ???? ??? ?? ??? ???? ?? ?????? ????? ??? ?? ?? ???????? ?? ????? ???? ???
????????? ????
System.Reflection??? ????? ???
????????? ???? ?? ??? ?????? ????????? ?? ?? ????? ??? ?? ?? ??????? ?? ???? ?????? ??????? ?? ?????? ???, ??????? ?????????? ???? ?????????? ????? ?? ???? ?????????, ?? ??? ?????? ??? ???? ????????? ?? ???????? ??? ???? ?? ?? ??? ?????????? ??????? misplace ??? ?? approach ?? ?? ???? ?? ?? ?? ???? ?????? ???????? ??????? ??? ?? ????? ?? ??? ????????? recompiling ???? ??? ??, ?? ???? ???? ?? ???????? ?????? ?? ??? ??? ???? ????????? ?? ?????? ?? ????? ???? ?????? ??????? ????? ?????
Step-by-Step ????????
????? ?? ??? ???????? ?????? ???? ????????? ?? ???, ???? ???? ?????? ?????? ?????? ???? ????????? ?? ?? ??? ?? ??? ??? ??? ?? ???? ????????? ?? ??? ??????? ??? ????? ??, ?? ??? ?? ?? ???? ??? ?? ????? ?? ???????? ?? ?????? ?? ?????????
System.Reflection??? ????? ???
???????? ?????? ??????
????? ?? ??? ?? ??? ????? ?? ???? ??? ???? ???? ????????? ?? ??? ???????? ???????? ?? ??? ???, ????? ????? ?? ???? ????:
- ?? ???????? ?? ??? ?? ??? Windows ????????? ????????? ?????? ????? ??? ?? ????? executing ??????? ?? ?????? ?????? ?? ????????? ???? ?? ??? ?? ??????? ?? ????? ???? ???? ???
- ???? ????????? ?? ??? ????-????? ????, ????? ????add?? ????-????? ????, ?? ???? ????? ???? ??? ?????.
- ???????? ????????? ????? ???, ?? ??? ??????? ?????????, ?? ????? MyTextFile.txt ???? ?????? ????? ??????? (IDE) ??? ????? ?? ????? ??, ?? ??? ??? ????? ??, ?? ???? ??? ????? ?? ??? ?????
- 1 ?? 2 ???? ????????? ?? ???, ????? ?? ??? ???? ?? ???? ?? ?????? ??? ?? ?????? ?? ??? ????? ?? ?????????? ???????? ???? ??????, ?? ??? ??? ??? ????????? ?? ??????, ?? ???? ??? ???? ?? ??? ????? ?? ??? ?????MyImage.bmp. ?? ??? IDE ??? ?? ???? ??, ?? ???? ??? ?? ?????? ????, ?? ????? ?? ??? ?????
- ??? ???? ?? ?????? ?? ????-????? ????, ?? ???? ??? ?? ??? ???????.
- ????????????? ????? ???, ???????????? ????????? ???????? ??? ??, ?? ??? ??? ?????????. ??? ?? ????? ???? ?? ????????? ?????????? ????????? ?? ??? ??????????? ??????.
- ???? ????? ?? ??? ??? 4 ?? 5 ??????? ?????
???? ??? ?? ?? ????????? ????? ??????? ?? ??????? ?? ?????? ?? ???? ???????? ??????? ????? ????????? ?? ??? ??? ????? ?????? ?? ??? ?? ????????? ??? ????? ??? ?????? ?? ???, ??? ???? ????????? ?? ??? ??? ????? ?? MyNamespace ??, ?? ?????? ??? ???? MyNamespace.MyTextFile.txt ?? MyNamespace.MyImage.bmp.
???:: ?????? ????? ??? ???-?????? ???? ???? ?? ?? ?????? ?? ????? ??, ?? ?? ????? ???? ????? ??? ?????? ?? ????? ??? ?? ??????? If you do not use the exact spelling and case of the file name, the method call to access the
ManifestResourceStreamreturns
??? ???? ??, and the system does not raise an exception.
???:: If you want to verify the resource names, you can use the Microsoft Intermediate Language Disassembler (ILDASM) to view the Manifest data, which lists the included resources.
Access Resources
To access the resources that you have embedded in the Manifest of your assembly, import the
System.IO??
System.Reflectionnamespaces, as follows:
using System.IO;
using System.Reflection;
The
System.IOnamespace provides the definition of a stream and the
System.Reflection??? ????? ????????? ???? ??
??????????? ?? ???? ??????? ??? ???????? ???????? ?? ??????? ?? ??? ??????? ?????? ????? ???
?? ?? ????? ??????? declaration ??????? ??? ?????, ??????? ?? ?????? ??????? ??? ???? ??? ?? ??? ???:
Assembly _assembly;
Stream _imageStream;
StreamReader _textStreamReader;
???:: ?? ?????
??? ??????????? ??? ?????? ???, ?? ??? ????? ??????? ?????? ?????? ??? ???-????? ?????
??????? ??? ????????? ?? ??? ?? ?? ??????? ?? ?????? ?? ???? ?? ??? ?? ??????? ?? ?? ?????? ??????? ???? ????? ??? ???? ?? ???, ?? ?????
GetExecutingAssemblyfollows ?? ??? ???, ??????? ????:
_assembly = Assembly.GetExecutingAssembly();
Reading the information from the resource to a stream is performed with a method call to
GetManifestResourceStream. The parameter that is passed to this method is the name of the resource that is to be accessed. The two resources are then read to their corresponding streams as the
??? ????event of the form is executed.
_imageStream = _assembly.GetManifestResourceStream("MyNameSpace.MyImage.bmp");
_textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNameSpace.MyTextFile.txt"));
The code in the
??? ????event for the form resembles the following:
try
{
_assembly = Assembly.GetExecutingAssembly();
_imageStream = _assembly.GetManifestResourceStream("MyNamespace.MyImage.bmp");
_textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNamespace.MyTextFile.txt"));
}
catch
{
MessageBox.Show("Error accessing resources!");
}
The
Try-Catchstatement, known as
structured error handlingin .NET, is used to catch any errors that may have occurred while the instance of the
Assembly???? ???????? ?? ??????? ???
???????? ??????
?? ?????? ???????? ???????? ?? ????????? ???? ?? ??? ?? ??? ?? ????? ???? ??? ?? ?? ???? ??? ????? ????, ??????? ?? ?? ?????? ?? ?????? ?? ?????? ??? ???? ?? ??? ????????? ???? ???? ??
PictureBox??????? ?? ???????? ??? ????? ??? ??? ?????? ?? ???? ?? ?? ???? ??? ????? ??? ??? ????????? ???? ???
???????? ???????? ?? ????????? ???? ?? ??? ????? ????? ?? ???? ????:
- ????? ???PictureBox????????? ???? ?? ??? ????????
- ??? ??? ???????????????? ?? ????????? ????, ?? ?? ????????? ???? ??????????? ?? ??? ?????? ??????.
- ????? ?? ??? ??? ?? ???-????? ???? ????????? ????,????? ?????? ?? ???, ?? ?? ?? ????? ??? ????? ??? ???????:
try
{
pictureBox1.Image = new Bitmap(_imageStream); }
catch
{
MessageBox.Show("Error creating image!");
}
?? ??? ?????? ??????? ??? ???? ?? ?? ?????? ?? ???? ?? ?????? ?? ?? ??? ?????? ????? ????? ????????? ?? ????????
- ????? ?????? ?????????????? ???? ?? ??? ????????
- ???? ??????????????? ?? ????????? ????, ?? ?? ????????? ???? ??????????? ?? ??? ?????? ?? ??????? ????.
- ?????? ?????? ??? ????? ?? ??? ??? ?? ???-????? ????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 ??????? ????????? ????? ???, ????? C# ????? ??????? ?? ?? ????????? ?? ??? ???????? ??? ??? ?? ?????? ?? Form1 ??? ???? ??? ??.. ??????? ?? ???????????? ???? ???? ?? ??????? ??? Form1.cs ?? Form1.designer.cs ??? ???? Form1.cs ??? ???? ??? ?????? Designer.cs ????? ?? ????? ?? ???? Windows ?????? ???????? ??? ????? ?? ?? ?? ??? ???????? ?? ???? ???? ?? ??????? ???? ???????? ???? ?? ???? ??..
????? C# 2005 ?? Visual Studio 2008 ??? Windows ??????? ??????? ?? ???? ??? ???? ??????? ?? ??? ????? Microsoft ??? ???? ?? ????:
?????? ??????
??????? ?????? ??? ???-?????? ???? ???, ?? ???????? ???? ?? ?? ????? ?? ??? ??? ?????? ?? ???????? ?? ?????? ??? ??? ?? ?? ??????? ???????? ?? ???? ?????? ?? ???? ???? ?? ??? ????????? ???? ????? ?? ??? ?? ILDASM ?? ????? ?? ?????
???? ID: 319292 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
- Microsoft Visual C# .NET 2002 Standard Edition
- Microsoft Visual C# 2005 Express Edition
- Microsoft Visual C# 2008 Express Edition
| kbsweptvs2008 kbhowtomaster kbmt KB319292 KbMthi |
???? ?????? ??????????????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:
319292
(http://support.microsoft.com/kb/319292/en-us/
)