【搬运】VB.NET创建和使用RESX资源

vb.net

VB.NET使用了一种新的资源格式。RESX和Resources。

这里介绍如何读取RESX格式。

 

 
  
Imports System.Resources
Imports System.IO
Public Class Form1
Dim rp As String = Application.StartupPath & " \MyRes.resx "
Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' 创建
Dim rw As New ResXResourceWriter(rp) ' 创建一个资源写入流
rw.AddResource( " res1 " , " 这是一个文本 " ) ' 添加一个资源(资源ID,资源数据) 写入文本
rw.AddResource( " res2 " , My.Resources.GIF_e001) ' 写入图像
rw.AddResource( " res3 " , New Byte () { 1 , 2 , 3 , 4 }) ' 写入数组
With My.Resources.Sou0 ' 对于特殊文件(比如流)进行序列化准备
Dim b(.Length - 1 ) As Byte ' 声明足够的缓冲区
.Read(b, 0 , .Length - 1 )
rw.AddResource(
" res4 " , b) ' 写入音频
.Close()
End With
' ……
rw.Close() ' 关闭流
MsgBox ( " OK " , MsgBoxStyle.Information)
End Sub
Private Sub Button2_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Not File.Exists(rp) Then Return
' 使用
Dim fr As FileStream = File.OpenRead(rp) ' 示例从流中读取资源
Dim rr As New ResXResourceReader(rp) ' 创建一个资源读取流
Dim id As IDictionaryEnumerator = rr.GetEnumerator ' 创建资源ID枚举
id.MoveNext() ' 开始枚举推进
MsgBox ( " 资源ID为: " & id.Entry.Key)
MsgBox ( " 资源类型为: " & id.Entry.Value.GetType.Name)
Me .Text = id.Value ' 使用字符 (请根据资源类型来使用资源)
id.MoveNext() ' 推进至下一资源
Me .BackgroundImage = id.Value ' 使用图像
id.MoveNext()
Dim b() As Byte = id.Value
MsgBox ( " 资源内的字节数组大小: " & b.Length) ' 使用数组
id.MoveNext()
b
= id.Value ' 获取音频数据字节组
Dim ms As New MemoryStream(b) ' 创建一个内存流
My.Computer.Audio.Play(ms, AudioPlayMode.Background) ' 播放这个音频
ms.Close() : rr.Close() ' 关闭内存流和资源流
MsgBox ( " OK! " , MsgBoxStyle.Information)
End Sub
End Class

转载于:https://www.cnblogs.com/clso/archive/2010/11/29/1891094.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值