vbnet的资源文件可以存取多个二进制文件吗? - 爱问答

(爱问答)

vbnet的资源文件可以存取多个二进制文件吗?

怎么弄??????????????????????????????

可以的;VB.Net中己不再使用Open来读写二进制文件,而是用BinaryReader/BinaryWriter来对二进制文件进行读写操作。

 

举例如下

Imports System.Windows.FormsImports System.IOImports System.Text Public Class Form1     '按下Button1按钮,创建一个二进制文件并写入一个浮点数    Private Sub Button1_Click(sender As Object, _                              e As EventArgs) Handles Button1.Click        '创建文件准备写入        Dim myFile As New IO.FileStream("d:data.bin", _                                        FileMode.Create, _                                        FileAccess.Write)        '写入二进制格式数据        Dim bw As New BinaryWriter(myFile)        '写入一个浮点数        Dim f As Single        f = 3.14159        bw.Write(f)        '关闭流        bw.Flush()        bw.Close()        '关闭文件        myFile.Close()    End Sub     '按下Button2按钮,从二进制文件并读入一个浮点数值并显示    Private Sub Button2_Click(sender As Object, _                              e As EventArgs) Handles Button2.Click                 '文件不如不存在则退出        If Not File.Exists("d:data.bin") Then Exit Sub                 '打开文件准备读        Dim myFile As New IO.FileStream("d:data.bin", _                                        FileMode.Open, _                                        FileAccess.Read)        '按二进制格式读取数据        Dim br As New BinaryReader(myFile)        '读取一个浮点数        Dim f As Single        f = br.ReadSingle()        '关闭流        br.Close()        '关闭文件        myFile.Close()        '显示读出的内容        MessageBox.Show(f.ToString())    End SubEnd Class




VB.NET打开二进制文件用fileopen完成,打开二进制文件的形式为:openmode.binary


读取二进制文件用的是fileget方法,写入二进制文件用的是fileput方法。


应用示例:将一批随机数保存在一个dat文件中,然后再将其提取到文本框中。


二进制文件的读写一批随机数的存取,程序为:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


Dim x, i, fn As Integer


Dim s As String = ""


fn = FreeFile()


FileOpen(fn, "d:data.dat", OpenMode.Binary)


For i = 1 To 8


x = Int(Rnd() * 100)


s = s + Str(x)


FilePut(fn, x)


Next


FileClose(fn)


TextBox1.Text = s


End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


Dim x, fn As Integer


Dim s As String = ""


fn = FreeFile()


FileOpen(fn, "d:data.dat", OpenMode.Binary)


Do While Not EOF(fn)


FileGet(fn, x)


s = s + Str(x) + " "


LoopFileClose(fn)


TextBox1.Text = s


End Sub

相关标签:资源

下一篇:重载圆柱滚动轴承使用寿命是多长时间

上一篇:初中毕业,没有任何编程基础,可以学软件开发吗?去哪个学校

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?