中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 登陆资料
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 未整理篇 | 技术讨论
如何得到资源文件中的文件
作者:未知 时间:2003-07-25 12:12 出处:互联网 责编:chinaitpower
              摘要:暂无

在应用程序里嵌入资源,可以避免用户因删除资源文件而造成应用程序出现错误。要使用资源文件中的文件,只需要按下面的方法调用即可:

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.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) ; '} End Module ' 主文件 Public Class Form1 Inherits 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 Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Windows 窗体设计器所必需的 Private components As System.ComponentModel.IContainer '注意: 以下过程是 Windows 窗体设计器所必需的 '可以使用 Windows 窗体设计器修改此过程。 '不要使用代码编辑器修改它。 Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Button4 As System.Windows.Forms.Button Friend WithEvents Button5 As System.Windows.Forms.Button Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents Button6 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.RichTextBox1 = New System.Windows.Forms.RichTextBox Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.Button4 = New System.Windows.Forms.Button Me.Button5 = New System.Windows.Forms.Button Me.Button6 = New System.Windows.Forms.Button Me.PictureBox1 = New System.Windows.Forms.PictureBox Me.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 = 0 Me.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 = 1 Me.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 = 2 Me.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 = 3 Me.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 = 4 Me.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 = 5 Me.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 = 7 Me.Button6.Text = "装载图片的例子2" ' 'PictureBox1 ' Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.PictureBox1.Cursor = System.Windows.Forms.Cursors.Default Me.PictureBox1.Location = New System.Drawing.Point(144, 104) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(360, 192) Me.PictureBox1.TabIndex = 6 Me.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.FixedSingle Me.MaximizeBox = False Me.Name = "Form1" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "得到资源文件中的文件" Me.ResumeLayout(False) End Sub #End Region Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click RichTextBox1.LoadFile(GetEmbeddedResource("GetEmbeddedResource.xsl.txt"), _ RichTextBoxStreamType.PlainText) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button2.Click Me.Icon = New Icon(GetEmbeddedResource("GetEmbeddedResource.1.ico")) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button3.Click Me.Icon = New Icon(GetEmbeddedResource("GetEmbeddedResource.2.ico")) End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button4.Click PictureBox1.Image = New Bitmap(GetEmbeddedResource("GetEmbeddedResource.rose.jpg")) End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button5.Click RichTextBox1.LoadFile(GetEmbeddedResource("GetEmbeddedResource.Text.rtf"), _ RichTextBoxStreamType.RichText) End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button6.Click PictureBox1.Image = New Bitmap(GetEmbeddedResource("GetEmbeddedResource.Test.jpg")) End Sub End Class
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有