在应用程序里嵌入资源,可以避免用户因删除资源文件而造成应用程序出现错误。要使用资源文件中的文件,只需要按下面的方法调用即可:
VB.net Function GetEmbeddedResource(ByVal strname As String) As System.IO.Stream
Return System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(strname)
End Function
C# System.IO.Stream GetEmbeddedResource(string strname){
return System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(strname) ;
}
其中的参数strname必须是下面的格式:..,比如: "MyApplication.Icon1.ico"。参数是大小写敏感的。如果不正确或者缺少文件,将会出现返回Null的错误。要在你的引用程序中嵌入文件,只需要按下面的步骤操作即可:1,在资源管理器里选中文件2,按住鼠标左键,拖到工程文件上,松开鼠标左键。3,在拖放的文件上点鼠标右键,选“属性”4,在生成操作里选择“嵌入的资源”。在进行测试之前,请准备好以下几个文件:1.ico,2.ico,Text.rtf,xsl.txt,rose.jpg,Test.jpg,结果如下:
下面是完整的例子:' 模块文件Module Module1'VB.NETFunction GetEmbeddedResource(ByVal strname As String) As System.IO.StreamReturn System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(strname)End Function'C#'System.IO.Stream GetEmbeddedResource(string strname) {'return System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(strname) ;'}End Module' 主文件Public Class Form1Inherits System.Windows.Forms.Form#Region " Windows 窗体设计器生成的代码 "Public Sub New()MyBase.New()'该调用是 Windows 窗体设计器所必需的。InitializeComponent()'在 InitializeComponent() 调用之后添加任何初始化End Sub'窗体重写 dispose 以清理组件列表。Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)If disposing ThenIf Not (components Is Nothing) Thencomponents.Dispose()End IfEnd IfMyBase.Dispose(disposing)End Sub'Windows 窗体设计器所必需的Private components As System.ComponentModel.IContainer'注意: 以下过程是 Windows 窗体设计器所必需的'可以使用 Windows 窗体设计器修改此过程。'不要使用代码编辑器修改它。Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBoxFriend WithEvents Button1 As System.Windows.Forms.ButtonFriend WithEvents Button2 As System.Windows.Forms.ButtonFriend WithEvents Button3 As System.Windows.Forms.ButtonFriend WithEvents Button4 As System.Windows.Forms.ButtonFriend WithEvents Button5 As System.Windows.Forms.ButtonFriend WithEvents PictureBox1 As System.Windows.Forms.PictureBoxFriend WithEvents Button6 As System.Windows.Forms.ButtonPrivate Sub InitializeComponent()Me.RichTextBox1 = New System.Windows.Forms.RichTextBoxMe.Button1 = New System.Windows.Forms.ButtonMe.Button2 = New System.Windows.Forms.ButtonMe.Button3 = New System.Windows.Forms.ButtonMe.Button4 = New System.Windows.Forms.ButtonMe.Button5 = New System.Windows.Forms.ButtonMe.Button6 = New System.Windows.Forms.ButtonMe.PictureBox1 = New System.Windows.Forms.PictureBoxMe.SuspendLayout()''RichTextBox1'Me.RichTextBox1.Location = New System.Drawing.Point(0, 0)Me.RichTextBox1.Name = "RichTextBox1"Me.RichTextBox1.Size = New System.Drawing.Size(512, 96)Me.RichTextBox1.TabIndex = 0Me.RichTextBox1.Text = "要在你的引用程序中嵌入文件,只需要按下面的步骤操作即可:" & _Microsoft.VisualBasic.ChrW(10) & _"1,在资源管理器里选中文件" & Microsoft.VisualBasic.ChrW(10) & _"2,按住鼠标左键,拖到工程文件上,松开鼠标左键。" & _Microsoft.VisualBasic.ChrW(10) & _"3,在拖放的文件上点鼠标右键,选""属性""" & _Microsoft.VisualBasic.ChrW(10) & _"4,在生成操作里选择""嵌入的资源""。"''Button1'Me.Button1.Location = New System.Drawing.Point(8, 104)Me.Button1.Name = "Button1"Me.Button1.Size = New System.Drawing.Size(128, 26)Me.Button1.TabIndex = 1Me.Button1.Text = "装载纯文本文件"''Button2'Me.Button2.Location = New System.Drawing.Point(8, 168)Me.Button2.Name = "Button2"Me.Button2.Size = New System.Drawing.Size(128, 26)Me.Button2.TabIndex = 2Me.Button2.Text = "更改窗体标题栏图标"''Button3'Me.Button3.Location = New System.Drawing.Point(8, 200)Me.Button3.Name = "Button3"Me.Button3.Size = New System.Drawing.Size(128, 26)Me.Button3.TabIndex = 3Me.Button3.Text = "更改窗体标题栏图标"''Button4'Me.Button4.Location = New System.Drawing.Point(8, 232)Me.Button4.Name = "Button4"Me.Button4.Size = New System.Drawing.Size(128, 26)Me.Button4.TabIndex = 4Me.Button4.Text = "装载图片的例子1"''Button5'Me.Button5.Location = New System.Drawing.Point(8, 136)Me.Button5.Name = "Button5"Me.Button5.Size = New System.Drawing.Size(128, 23)Me.Button5.TabIndex = 5Me.Button5.Text = "装载RTF文件"''Button6'Me.Button6.Location = New System.Drawing.Point(8, 264)Me.Button6.Name = "Button6"Me.Button6.Size = New System.Drawing.Size(128, 26)Me.Button6.TabIndex = 7Me.Button6.Text = "装载图片的例子2"''PictureBox1'Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3DMe.PictureBox1.Cursor = System.Windows.Forms.Cursors.DefaultMe.PictureBox1.Location = New System.Drawing.Point(144, 104)Me.PictureBox1.Name = "PictureBox1"Me.PictureBox1.Size = New System.Drawing.Size(360, 192)Me.PictureBox1.TabIndex = 6Me.PictureBox1.TabStop = False''Form1'Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)Me.ClientSize = New System.Drawing.Size(506, 303)Me.Controls.Add(Me.PictureBox1)Me.Controls.Add(Me.Button6)Me.Controls.Add(Me.Button5)Me.Controls.Add(Me.Button4)Me.Controls.Add(Me.Button3)Me.Controls.Add(Me.Button2)Me.Controls.Add(Me.Button1)Me.Controls.Add(Me.RichTextBox1)Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingleMe.MaximizeBox = FalseMe.Name = "Form1"Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreenMe.Text = "得到资源文件中的文件"Me.ResumeLayout(False)End Sub#End RegionPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _Handles Button1.ClickRichTextBox1.LoadFile(GetEmbeddedResource("GetEmbeddedResource.xsl.txt"), _RichTextBoxStreamType.PlainText)End SubPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _Handles Button2.ClickMe.Icon = New Icon(GetEmbeddedResource("GetEmbeddedResource.1.ico"))End SubPrivate Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _Handles Button3.ClickMe.Icon = New Icon(GetEmbeddedResource("GetEmbeddedResource.2.ico"))End SubPrivate Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _Handles Button4.ClickPictureBox1.Image = New Bitmap(GetEmbeddedResource("GetEmbeddedResource.rose.jpg"))End SubPrivate Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _Handles Button5.ClickRichTextBox1.LoadFile(GetEmbeddedResource("GetEmbeddedResource.Text.rtf"), _RichTextBoxStreamType.RichText)End SubPrivate Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _Handles Button6.ClickPictureBox1.Image = New Bitmap(GetEmbeddedResource("GetEmbeddedResource.Test.jpg"))End SubEnd Class