Problems with Filestream

Lew

New member
Joined
Oct 25, 2024
Messages
1
Programming Experience
10+
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

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
This line:
VB.NET:
fs.Read(arPlainText, 0, FileLen(strFilename) + 1)
throws this exception:
System.InvalidCastException: 'Unable to cast object of type 'System.Byte' to type 'System.Byte[]'.'
 
Last edited by a moderator:
Set Option Strict On in your project properties, then fix the errors that are flagged as a result. The obvious issue in that code is that none of your method parameters have a type declared, so they will all default to Object. You are presumably passing a Byte value to that parameter where you should be passing a Byte array object, which will be enforced by the compiler if you do as you should always do and declare a type for every variable, etc.

Once you've done that, if you still have an issue, post back and provide an example of how the code is invoked to generate the error too. The code you have could work if invoked properly so how you're invoking it is relevant to the problem. While it's not a requirement here like it is at Stack Overflow, you should ALWAYS strive to provide a minimal, reproducible example of the problem. Minimal means the least code possible to demonstrate the issue, which will often not be just a copy and paste of the code form your project, which may include lots that is irrelevant to the problem you're trying to address. Reproducible means that we should be able to run the code you provide and see the behaviour you describe. Any guessing or assumptions on our part make it more likely that we're addressing something other than what yo0u're describing.

Once you have set Option Strict On in the project settings, you should do it in the VS options too, so it will be On by default for all future projects.
 
Hey! The issue seems to be a mismatch in data types between arSeed and arPlainText. When using FileStream.Read, make sure you're passing a correctly defined Byte() array length. Try applying DirectCast on arPlainText to enforce the data type. You could also try Buffer.BlockCopy if casting issues persist. Another thing is check that lnptFileLen file length is correctly assigned before reading. Let me know if it helps :)
 
Back
Top