① vb.net 二进制读取文件
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) + " "
Loop
FileClose(fn)
TextBox1.Text = s
End Sub
② 我用二进制读取了声音文件,VB或VB.net如何播放二进制声音数据,请高手赐教!
不明白你为什么要二进制读取声音文件,VB可以直接调用API播放声音文件,不需要你二进制来读取声音文件的。
Private
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA"
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub PlayWavFile(strFileName As String, PlayCount As Long, JianGe As Long)
'strFileName 要播放的文件名(带路径)
'playCount 播放的次数
'JianGe 多次播放时,每次的时间间隔
If Len(Dir(strFileName)) = 0 Then Exit Sub
If PlayCount = 0 Then Exit Sub
If JianGe < 1000 Then JianGe = 1000
DoEvents
sndPlaySound strFileName, 16 + 1
Sleep JianGe
Call PlayWavFile(strFileName, PlayCount - 1, JianGe)
End Sub
Private Sub Form_Click()
PlayWavFile "C:\aaa.wav", 1, 1000 '参数:播放文件,播放次数,播放间隔
End Sub
③ VB.NET怎样从一个二进制文件中读取数据并进行数据转换后写入另一个文件
system.text.Encoding.getEncoding("unicode").toString(Byte())
④ vb.net 将文件转化成二进制
将文件转化成二进制
Dim data As Byte() = File.ReadAllBytes("a.jpg")
将数组再转化版成图权片
Dim img As Image
Using ms As New MemoryStream(data)
img = Image.FromStream(ms)
End Using
⑤ .net 二进制读写文件(vb, C#均可)
数据量不大的话,直接使用如下函数
byte[] bytes = File.ReadAllBytes("文件路径专"); // 读二进制数属据
File.WriteAllBytes("文件路径", bytes); // 写二进制数据
⑥ VB.NET 怎么读写二进制文件,类似Open
'从文件指定位置读取数据
dim fn as new io.filestream("e:\123.mp3", IO.FileMode.Open)
dim fr as new io.binaryreader(fn)
fr.BaseStream.Position = 1000 '从1000字节处开始读取文件
dim data() as byte= fr.ReadBytes(2000) '读取2000个字节至data()数组
fr.close
fn.close
'在文件指定位版置写权入数据
dim fn as new io.filestream("e:\123.mp3", IO.FileMode.Open)
dim fr as new io.binarywriter(fn)
fr.BaseStream.Position = 1000 '从1000字节处开始读取文件
fr.write(data) '将data数组写入文件
fr.close
fn.close
'关键在于这句:fr.BaseStream.Position = 1000 ,即将文件流的指针移动到1000字节处,然后再根据自己的要求操作即可。
⑦ 请问vb.net中怎样把数组写入二进制文件呢
使用My命名空知间
例子如道下:
Dim myData() As Byte
myData = System.Text.Encoding.Unicode.GetBytes("<html><head></head><body>Hello World!</body></html>")
My.Computer.FileSystem.WriteAllBytes("c:\helloworld.html", myData, True) '写入文专件内容属
myData = My.Computer.FileSystem.ReadAllBytes("c:\helloworld.html") ‘读取文件
⑧ vb.net 中打开二进制文件并转换成16进制
Dim fn As Integer, b() As Byte, strOut As String
fn = FreeFile()
FileOpen(fn, "filename", OpenMode.Binary, OpenAccess.Read, OpenShare.Shared)
ReDim b(LOF(fn) - 1)
FileGet(fn, b)
FileClose(fn)
Dim i As Integer
For i = LBound(b) To UBound(b)
strOut = strOut & Hex(b(i)) & " "
Next i
不知道你是不是这个意思内哈。容。
⑨ vb.net如何读写二进制文件 有各种类型的变量
使用My命名空间
例子如下:
Dim myData() As Byte
myData = System.Text.Encoding.Unicode.GetBytes("<html><head></head><body>Hello World!</body></html>")
My.Computer.FileSystem.WriteAllBytes("c:\helloworld.html", myData, True) '写入文件
myData = My.Computer.FileSystem.ReadAllBytes("c:\helloworld.html") ‘读专取属文件
⑩ VB.NET的二进制文件读写为什么这么慢比C++的慢慢N倍啊
这个是你的问题,不是vb.net速度慢。
你应该这样写:
Dim file1 As FileStream = New FileStream("地址", FileMode.Open)
Dim myread As BinaryReader = New BinaryReader(file1)
Dim fn(file1.Length) As Byte
For i As Integer = 0 To file1.Length - 1
fn(i) = myread.ReadByte
Next
记得前面要 imports system.io
这个代码把文件读入一个fn的数组中,速度极快。