se c'era vs2005, allora c'era pure il metodo
Public Shared Function ReadAllBytes(path As String) As Byte()
che legge un file e restituisce un vettore di byte.
Guardando come è fatta con un reflector si vede che usa il costrutto using
perciò, secondo me, basta usare quel metodo.codice:Public Shared Function ReadAllBytes(path As String) As Byte() Return File.InternalReadAllBytes(path, True) End Function Private Shared Function InternalReadAllBytes(path As String, checkHost As Boolean) As Byte() Dim array As Byte() Using Dim fileStream As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.None, Path.GetFileName(path), False, False, checkHost) Dim num As Integer = 0 Dim length As Long = fileStream.Length If length > 2147483647L Then Throw New IOException(Environment.GetResourceString("IO.IO_FileTooLong2GB")) End If Dim i As Integer = CInt(length) array = New Byte(i - 1) {} While i > 0 Dim num2 As Integer = fileStream.Read(array, num, i) If num2 = 0 Then __Error.EndOfFile() End If num += num2 i -= num2 End While End Using Return array End Function
In alternativa, facendo finta che non esista quel metodo, prova questo (usato con vs precedente)
codice:'------------------------------------------------------------- 'leggo un file e ne restituisco un vettore di byte 'esempio: 'Dim buffer As Byte() = ReadBinaryFile("c:\tmp\prova.txt") 'WriteBinaryFile("c:\tmp\prova1.txt", buffer) '------------------------------------------------------------- Public Function ReadBinaryFile(ByVal nomeFile As String) As Byte() Dim st As Stream = Nothing Dim br As BinaryReader = Nothing Dim buffer() As Byte Try 'associo uno stream ad un nuovo file aperto in modalità di lettura st = File.Open(nomeFile, FileMode.Open, FileAccess.Read) 'creo un binaryReader associato allo stream di output br = New BinaryReader(st) ReDim buffer(CInt(st.Length - 1)) 'leggo i dati nel buffer br.Read(buffer, 0, CInt(st.Length)) Return buffer Catch ex As Exception Throw Finally 'chiudo il file If Not (br Is Nothing) Then br.Close() If Not (st Is Nothing) Then st.Close() End Try End Function

Rispondi quotando
Sto scherzando, grazie mille invece!
hai ragione, ma ho provato il tuo codice e mi funzionava 