????? ????? ?????? ??? ??????? ???????? Visual C#

?????? ????????? ?????? ?????????
???? ???????: 319292 - ??? ???????? ???? ????? ????? ??? ???????.
????? ???? | ?? ????

?? ??? ??????

??????

???? ??? ??????? ???? ????? ??? ????? ??????? Visual C# ????? ??????? ???? ?? ??????? ? ?? ?????? ??? ??????? ?? ??? ???????.

???? ???? ???

???? ?? .NET Framework ???? ??????? ???? ?? ??????? ????????. ????? ??? ??????? ??? ??????? ???????. ??? ??????? ?????? ?????? ?? ??????? .resources ? .resx ???????? ?? ???????. ????? ?????? ??? ??? ??????? ?? ??? ??????? ???? ????? ??????? ?? ????? ????? System.Reflection.

????? ?????? ?? ????? ??????? ???? ?? ?? ??? ??????? ????? ?? ????? ???????? ???????? ?? ????? ????? ???? ?? misplace ??????? ?????? ???????? ???? ?? ??? ??????? ?? ??? ????? ????????. ????? ???? ?? ??? ??????? ??? ?? ????? ??? ??? ??????? ??? ??? ????? ??? ??????? ???? ????? ????????. ????? ???? ??? ????? ??????? ?? ??? ????? ????? ??? ??????? ????? ?? ????? ?????.

????? ???????? ???? ?????

?????? ??????? ??????? ??? ???????? ??? ????? ????? ??????? ???? ?? ???????. ??? ????? ??????? ??? ??????? ???? ?????? ??? ??????? ?? ???? ????? ????? System.Reflection ????.

????? ????? ??????

?????? ??? ??? ?? ??? ???? ??? ??????? ?????? ??????? ???? ??????? ???????:
  1. ????? ????? ????? Windows ???? ??? ????? ????????. ?????? ??? ??????? ?? ??? ??????? ???? ??? ?????? ????? ?? ????? ??????? ????? ??? ???????.
  2. ???? ??? ?????? ?????? ??? ??? ??????? ???? ??? ????? ?? ???? ??? ????? ???? ????.
  3. ?? ???? ?????? "???? ????" ? ??? Text File ?? ??????? ?? ?? ?????? ????? MyTextFile.txt. ??? ??? ????? ?? ???? ??????? ????????? (IDE) ????? ??? ???? ?? ?? ?????? ?????.
  4. ??? ???????? 1 ? 2 ?????? ???? ??? ??????? ???? ????? ?? ????? ??? ??? ??? ???? ???? ? ??? ??? ???? ????? ?? ?? ?????? ??? ????? ??? MyImage.bmp. ??? ??? ?????? ??????? ?? IDE ??? ??? ?? ??? ?????? ?? ?? ?????? ?????.
  5. ???? ??? ?????? ?????? ??? ????? ????? ?? ?????? ??????? ?? ??? ?????.
  6. ?? ???? ?????? "?????" ? ??? ???? ??????? "????? ?????". ?????????? ??? ????? ??? ??????? ??? ???????. ???? ??? ??????? ?? ????? ??????? ????? ????? ??? ??????? ???????.
  7. ??? ???????? 4 ?5 ??? ???.
?? ????? ??????? ???? ?? ????? ??????? ?? ?????? ??????? ???? ??? ??????? ??? ??????? ????? ??. ???? ?????? ??????? ????? ??? ????? ??????? ??? ??? ?????? ????? ??? ?????? ?? ???????. ??? ???? ??????? ??? ???? ????? ????? ????? ?? ??????? MyNamespace ? ??? ????? ??????? MyNamespace.MyTextFile.txt ? MyNamespace.MyImage.bmp.

??????: ????? ??????? ?????? ????? ????? ??????. ??? ?????? ??? ??????? ???? ??? ???? ??????? ???? ?????? ????? ??? ?????. ?? ??? ?? ??? ?????? ???? ?????? ?????? ?????? ??? ????? ??????? ??????? ?????? ManifestResourceStreamNothing ????? ?????? ???????.

??????: ??? ??? ???? ?????? ?? ????? ??????? ????? ??????? ?? Microsoft ????? ????? Disassembler (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!");
   }
				
??? ??????? ????? ???? ?????? ???? ?????? ????? ????? ?? .NET ? ???? ??? ????? ?? ???? ??? ????? ?????? ???? ??????? ??????? ??? ???????.

??? ???????

?????? ??? ?????? ???? ???? ??????? ???????. ??? ????? ??? ???? ????? ??? ????? ???? ????? ??? ???? ?????? ?? ??????? ?????? ?? ???? ?????? PictureBox ???????. ???? ?????? ?? ??????? ?? ??? ????? ?? ????? ???? ?? ???? ??.

???? ??????? ??????? ???? ??????? ???????:
  1. ????? ???? ???? PictureBox ??? ???????.
  2. ????? ???? ???? ?? ???? ??? ??????? ?? ?? ?????? ??????? Text ????? ?? ??? ????? ??????.
  3. ???? ????? ??????? ??? ?? ??? ????? ????? ?? ???? ?? ???? ????????? ???????? ?? ?? ???? ???????? ???????? ??????? ?? ??? ?????:
       try
       {
          pictureBox1.Image = new Bitmap(_imageStream);			       }
       catch 
       {
          MessageBox.Show("Error creating image!");
       }
    					
    ????????? ???????? ??? ?????? ???? ???? ?? ?????? ??????? ???? ????? ??? ??? ?????? ???? ??? ??????? ?? ????? Load ???????.

  4. ????? ???? ???? ???? ?? ??? ???????.
  5. ????? ???? ???? ?? ??? ??? ??????? ?? ?? ?????? ??????? Text ????? ?? ??? ????? ??.
  6. ???? ????? ??????? ??? ???? ?? ???? ????? ???? Click_Event ??? ?? ?? ???? ???????? ???????? ??????? ?? ?????:
       try
       {
          if(_textStreamReader.Peek() != -1)
          {
             textBox1.Text = _textStreamReader.ReadLine();
          }
       }
       catch
       {
          MessageBox.Show("Error writing text!");
       }
    					
    ??? ????????? ???????? ???? ?? ??? ???? ?????? ???? ????? ?????? ?????? ?? ?????. ??? ?? ?????? ??? ???? ?? ??????? ??? ???? ????.

  7. ???? 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 ?? ?? 2008 Visual Studio. ??? ????? ????? Windows ????? Visual C# ????? ????? ??? ??????? ???? ???????. ???? ??? ??????? Form1. ??? ????? ??????? ???? ???? ??????? Form1.cs ? Form1.designer.cs. ????? ????????? ???????? ?? Form1.cs. ??? ????? Designer.cs ??? ???? & ???? ????? Windows ????????? ???????? ???? ???? ?????? ???? ????????? ????? ?? ???? ????? ????? ????. ?????? ??? ???? ?? ????????? ??? ???? ????? Windows ?? Visual C# 2005 ?? ?? 2008 Visual Studio ?? ?????? ???? Microsoft ?????? ??? ?????:
http://msdn2.microsoft.com/en-us/library/ms173077.aspx

??????? ??????? ????????

??? ????? ????? ??????? "? ???? ?? ??? ?????? ??????? ?????? ????? ??????? ???? ??? ?????? ?????. ????? ??????? ILDASM ????? ?????? ???? ?????? ?? ???? ?????? ???????.

?????

????? ?? ?????????? ???? ????? ???? ????? Microsoft (MSDN) ?????? ??? ?????:
????? ????? System.Reflection
http://msdn2.microsoft.com/en-us/library/system.reflection(vs.71).aspx

??? ???????
http://msdn2.microsoft.com/en-us/library/system.reflection.assembly(vs.71).aspx

????? Assembly.GetManifestResourceStream
http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.getmanifestresourcestream(vs.71).aspx

???????

???? ???????: 319292 - ????? ??? ??????: 12/???/1429 - ??????: 3.1
????? ???
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
  • Microsoft Visual C# 2008 Express Edition
????? ??????: 
kbmt kbsweptvs2008 kbhowtomaster KB319292 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????319292

????? ???????

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com