VB.Net获取资源文件中文件的方法
2012-10-20 10:39
513 查看
在应用程序里嵌入资源,可以避免用户因删除资源文件而造成应用程序出现错误。要使用资源文件中的文件,只需要按下面的方法调用即可:
Function GetEmbeddedResource(ByVal strname As String) As System.IO.Stream
Return System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(strname)
End Function
其中的参数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
Function GetEmbeddedResource(ByVal strname As String) As System.IO.Stream
Return System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(strname)
End Function
其中的参数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
相关文章推荐
- VB.NET获取文件默认图标的方法
- [VB.NET]哪里可以找到用于VB.NET2005的菜单资源图片文件呢?在visio studio中有吗?
- Liferay Dynamic CSS Filter方法的研究 - 从请求文件URI到获取资源文件真实路径
- asp.net获取当前页面文件名,参数,域名等方法。统一session验证和权限验证的方法
- [转载]VB.NET下获取硬盘信息的几种方法
- VB获取文件大小的方法
- VB.net对资源文件的调用
- ASP.NET获取IP及电脑名等信息的简单方法+通用类文件源码
- VB.NET获取硬盘信息的几种方法
- ASP.NET中将声音文件添加到资源中并进行播放的方法
- ASP.NET中将声音文件添加到资源中并进行播放的方法
- ASP.NET 三种获取指定文件路径下所有文件的文件名和路径的方法
- vb.net 判断文件编码的方法
- c#.net各种应用程序中获取文件路径的方法
- vb.net 教程 3-12 资源文件 1-1
- VB.NET获取硬盘信息的几种方法
- android从strings资源文件获取,并拼接字符串方法
- android中从values/strings资源文件中获取拼接字符串的方法
- C#&VB.NET自动添加文件注释的方法
- asp.net 从资源文件中获取资源内容、获取资源列表