2009-09-20 12 views
6

Come faccio a leggere un array di byte grezzo da qualsiasi file ...VB - Come faccio a leggere e scrivere un file binario?

Dim bytes() as Byte 

..e poi scrivere che matrice di byte di nuovo in un nuovo file?

Ho bisogno di un array di byte per eseguire qualche elaborazione in mezzo.


Attualmente sto usando:

Per leggere

Dim fInfo As New FileInfo(dataPath) 
Dim numBytes As Long = fInfo.Length 
Dim fsAs New FileStream(dataPath, FileMode.Open, FileAccess.Read) 
Dim br As New BinaryReader(fs) 
Dim bytes As Byte() = br.ReadBytes(CInt(numBytes)) 
br.Close() 
fs.Close() 

Per scrivere

Dim fs As System.IO.FileStream 
fs = New System.IO.FileStream(outpath, System.IO.FileMode.Create) 
fs.Write(bytes, 0, bytes.Length) 
fs.Close() 
+0

che ne pensi di pubblicare il tuo tentativo? –

+0

Simile a http://stackoverflow.com/questions/1450542/c-how-do-i-read-and-write-a-binary-file ... ma diverso poiché C# può fare cose che VB.NET puo ' t. –

+1

"C# può fare cose che VB.NET non può" - cura di nominarne alcuni, oltre ad alcuni aspetti della gestione XML? –

risposta

15
Dim data() as Byte = File.ReadAllBytes(path1) 
File.WriteAllBytes(path2, data) 
5
System.IO.File.ReadAllBytes("myfile.txt") 
3

Prova questa: -

Dim bytes() as Byte 
bytes = File.ReadAllBytes(fileName) 
'' # Do stuff to the array 
File.WriteAllBytes(otherFileName, bytes) 
Problemi correlati