none
GetfileContent Function from VB 6 to vb.net RRS feed

  • Question

  • I am upgrading a VB6 project to VB.net and  am stuck on this last function. As this must be able to handle all file types it is read in binary but gets written as string(I think). I have tried using the 2 examples from the MS website for binary reading but have been unsuccesful. It should be simple but my mind has gone Homer S.

     Private Function GetFileContents(ByVal strPath As String) As String
        Dim strBuff As String
           Open strPath For Binary As #1
            strBuff = Space(LOF(1))
        Get #1, , strBuff
        Close #1
            GetFileContents = strBuff
        Exit Function
        ERR_HANDLER:
              MsgBox(Err.Description)
              Err.Clear()
         End Function

    Thanks

    Sunday, January 28, 2018 9:47 PM

All replies

  • I am upgrading a VB6 project to VB.net and  am stuck on this last function. As this must be able to handle all file types it is read in binary but gets written as string(I think).

    That code is reading a file into a string buffer using a technique that gets all the bytes in the file, bypassing any special processing for a text file.   The string buffer probably requires special processing - there is likely code that examines it one character at a time, possibly converting each character into an Integer or Byte. 

    That won't work in .Net becasue a string is always Unicode.   You can get an array of bytes instead, and process each byte, instead of each character.  The equivalent is probably ReadAllBytes.  See:
    https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/drives-directories-files/how-to-read-from-binary-files

    If part of that file really needs to be a string you can convert a byte array (or part) into a string:
    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/strings/how-to-convert-an-array-of-bytes-into-a-string

    Sunday, January 28, 2018 9:57 PM
  • Hi

    Maybe something like this:

    Dim StrangeString As String = GetFileContents("C:\Users\lesha\Documents\Visual Studio 2017\Projects\AnimatedGIF4\AnimatedGIF4\bin\Debug\AnimatedGIF4.exe")
    
    
    
      Private Function GetFileContents(ByVal strPath As String) As String
    	Return System.Text.Encoding.UTF8.GetString(IO.File.ReadAllBytes(strPath))
      End Function


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, January 28, 2018 11:00 PM Changed encoding
    Sunday, January 28, 2018 10:09 PM
  • I think this is what might work for me. Nor sure if it will handle binary files like images or exe but is similar to the example which loads the string into PictureViewer.

    Private Function GetFileContents(ByVal strPath As String) As String
            Dim fs
            fs = IO.File.Open(strPath, IO.FileMode.Open)
            GetFileContents = fs
            Exit Function
            'ERR_HANDLER:
            MsgBox(Err.Description)
            Err.Clear()
            ' End Function


        End Function

    Monday, January 29, 2018 3:14 AM
  • I think this is what might work for me.

    The Open method returns a FileStream, not a String. You need to use the FileStream in order to get the file contents. 
    https://msdn.microsoft.com/en-us/library/system.io.filestream(v=vs.110).aspx

    You can read the contents as string but I don't think that's what you want.   I think you are much better off ignoring strings entirely, and making whatever other changes are required to process an array of bytes istead of a string.

    .Net error handling doesn't work like that.
    https://msdn.microsoft.com/en-us/library/ms973849.aspx

    Monday, January 29, 2018 4:47 AM
  • I am upgrading a VB6 project to VB.net and  am stuck on this last function. As this must be able to handle all file types it is read in binary but gets written as string(I think). I have tried using the 2 examples from the MS website for binary reading but have been unsuccesful. It should be simple but my mind has gone Homer S.

    Hi Hammer,

    The following example reads a UTF-8 encoded string from a binary file represented by a FileStream object.

    Const MAX_BUFFER_SIZE As Integer = 2048
    
       Dim enc8 As Encoding = Encoding.UTF8
    
       Public Sub Main()
          Dim fStream As New FileStream(".\Utf8Example.txt", FileMode.Open)
          Dim contents As String = Nothing
    
          ' If file size is small, read in a single operation.
          If fStream.Length <= MAX_BUFFER_SIZE Then
             Dim bytes(CInt(fStream.Length) - 1) As Byte
             fStream.Read(bytes, 0, bytes.Length)
             contents = enc8.GetString(bytes)
          ' If file size exceeds buffer size, perform multiple reads.
          Else
             contents = ReadFromBuffer(fStream)
          End If
          fStream.Close()
          Console.WriteLine(contents)
       End Sub   
    
        Private Function ReadFromBuffer(fStream As FileStream) As String
            Dim bytes(MAX_BUFFER_SIZE) As Byte
            Dim output As String = String.Empty
            Dim decoder8 As Decoder = enc8.GetDecoder()
    
            Do While fStream.Position < fStream.Length
               Dim nBytes As Integer = fStream.Read(bytes, 0, bytes.Length)
               Dim nChars As Integer = decoder8.GetCharCount(bytes, 0, nBytes)
               Dim chars(nChars - 1) As Char
               nChars = decoder8.GetChars(bytes, 0, nBytes, chars, 0)
               output += New String(chars, 0, nChars)                                                     
            Loop
            Return output
        End Function

    More detailed info, please refer to:

    https://msdn.microsoft.com/en-US/library/744y86tc.aspx

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, January 29, 2018 6:57 AM
    Moderator
  • Yea that is real old code, it is not typical VB6 but from the first  MSBasic supplied with ms Dos.

    Since VB7.0 there is a binaryreader so I doubt you found it on a MS website.

    https://msdn.microsoft.com/en-us/library/system.io.binaryreader(v=vs.110).aspx


    Success Cor

    Monday, January 29, 2018 8:41 AM