I am having an aggravating problem with Filestream
I am working with two publicly declared byte arrays, arSeed with a fixed length, and arPlainText of variable length, initially set to 255
I have one subroutine that reads the contents of a selected file into the arSeed array
This code works perfectly!
...
Another subroutine reads bytes from a selected file into the arPlainText array, after redimensioning arPlainText to the length of the file lnptFileLen. The code for reeding the file into arPlainText using fs is identical to that for reading data into arSeed, except for size, but throws the exception "unable to cast object of tyepe 'System Byte' to type 'System.Byte[]'
Anybody have any idea why these two nearly identical code segments don't both work?
This line:
throws this exception:
I am working with two publicly declared byte arrays, arSeed with a fixed length, and arPlainText of variable length, initially set to 255
VB.NET:
Public arSeed(intNRotors) As Byte
Public arPlaintext(255) As Byte
I have one subroutine that reads the contents of a selected file into the arSeed array
VB.NET:
Dim OpenKey As OpenFileDialog = New OpenFileDialog
OpenKey.DefaultExt = "Key"
OpenKey.FileName = ""
OpenKey.InitialDirectory = strKeyPath
OpenKey.Filter = "Key files|*.key|All files|*.*"
OpenKey.Title = "Select Key File"
OpenKey.RestoreDirectory = True
If OpenKey.ShowDialog() <> DialogResult.Cancel Then
Using fs As New FileStream(OpenKey.FileName, FileMode.Open, FileAccess.Read, FileShare.None)
fs.Read(arSeed, 0, intNRotors + 1)
End Using
End If
This code works perfectly!
...
Another subroutine reads bytes from a selected file into the arPlainText array, after redimensioning arPlainText to the length of the file lnptFileLen. The code for reeding the file into arPlainText using fs is identical to that for reading data into arSeed, except for size, but throws the exception "unable to cast object of tyepe 'System Byte' to type 'System.Byte[]'
Anybody have any idea why these two nearly identical code segments don't both work?
VB.NET:
Private Sub GetNewPlainText(ByRef arPlainText, ByRef lnptFileLen, ByRef strFilename)
Dim strSafeName As String = ""
Dim strPlainPath As String = "c:/users/mcint/onedrive/documents/enigma/plain"
Dim OpenPlain As OpenFileDialog = New OpenFileDialog
ReDim arPlainText(lnptFileLen)
OpenPlain.DefaultExt = "*.*"
OpenPlain.FileName = ""
OpenPlain.InitialDirectory = strPlainPath
OpenPlain.Filter = "All files|*.*|Text files|*.txt"
OpenPlain.Title = "Select Plain Text File"
OpenPlain.RestoreDirectory = True
If OpenPlain.ShowDialog() <> DialogResult.Cancel Then
strFilename = OpenPlain.FileName
lnptFileLen = FileLen(strFilename)
Using fs As New FileStream(OpenPlain.FileName, FileMode.Open, FileAccess.Read, FileShare.None)
fs.Read(arPlainText, 0, FileLen(strFilename) + 1)
End Using
boPT = True
End If
End Sub
VB.NET:
fs.Read(arPlainText, 0, FileLen(strFilename) + 1)
System.InvalidCastException: 'Unable to cast object of type 'System.Byte' to type 'System.Byte[]'.'
Last edited by a moderator: