我在以下代码
中收到错误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
答案 0 :(得分:3)
ReadBytes函数返回一个字节数组,您将传递给Convert.ToByte函数,该函数在运行时抛出异常,因为您无法将多个字节的数组转换为单个字节。根据您要完成的操作,修复问题的操作会有所不同。
答案 1 :(得分:1)
从您的函数实现中,很明显您想要返回所有字节。因此,更改ReadFile以返回Byte()并删除ToByte方法调用。