分享
 
 
 

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

王朝other·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

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

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有