发新话题
打印

如何得到资源文件中的文件

如何得到资源文件中的文件

  在应用程序里嵌入资源,可以避免用户因删除资源文件而造成应用程序出现错误。要使用资源文件中的文件,只需要按下面的方法调用即可:
  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 窗体设计器所必需的
  ** 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()> ** 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 = "要在你的引用程序中嵌入文件,只需要按下面的步骤操作即可:"

TOP

发新话题