![](/img/trans.png)
[英]Unable to cast object of type 'system.byte ' to type 'system.iconvertible in vb.net
[英]Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'
我在下面的代碼中得到錯誤
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
ReadBytes函數返回一個字節數組,您將其傳遞給Convert.ToByte函數,該函數在運行時拋出異常,因為您無法將多個字節的數組轉換為單個字節。 根據您要完成的操作,解決問題的操作會有所不同。
從您的函數實現中,很明顯您想要返回所有字節。 因此,更改ReadFile以返回Byte()並刪除ToByte方法調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.