Reputation: 21
i get the error in following code
Function ReadFile(ByVal sPath As String) As Byte
Dim data As Byte
data = Nothing
Dim fInfo As FileInfo
fInfo = New FileInfo(sPath)
Dim numBytes As Long
numBytes = fInfo.Length
Dim fStream As FileStream
fStream = New FileStream(sPath, FileMode.Open, FileAccess.Read)
Dim br As BinaryReader
br = New BinaryReader(fStream)
data = Convert.ToByte(br.ReadBytes(numBytes)) `getting error on this line`
Return data
End Function
Upvotes: 1
Views: 6449
Reputation: 21684
From your function implementation, it is obvious that you want to return all bytes. Therefore, change ReadFile to return Byte() and remove the ToByte method call.
Upvotes: 1
Reputation: 1039528
The ReadBytes function returns a byte array which you are passing to the Convert.ToByte function which throws an exception at runtime because you cannot convert an array of multiple bytes to a single byte. Depending on what you are trying to accomplish the actions to fix the problem will vary.
Upvotes: 3