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