Skip to content

VBMAN.FileEx - Advanced File Operation Object ​

Overview ​

VBMAN.FileEx provides advanced file operation functionality, implemented based on VB's native Open statement, supporting multiple encoding formats, flexible data read/write methods, and buffer operations.

Core Features ​

  • Multi-encoding Support: UTF-8, ANSI, UTF-16LE auto-detection
  • Flexible Modes: Read-only, write-only, read-write modes
  • Buffer Operations: Supports byte arrays, Base64, Hex, and other formats
  • Chain Calling: Fluent API design
  • Large File Support: Supports file operations up to 2GB

Properties ​

PropertyTypeDescription
IsOpenBooleanWhether file is open
FilePathStringCurrently open file path
LastErrorStringLast error message
FileSizeCurrencyFile size (bytes)
BufferSizeLongCurrent buffer byte count

Methods ​

File Open/Close ​

OpenFile ​

Open file

vb
Public Function OpenFile(ByVal FilePath As String, Optional ByVal AccessMode As String = "RW") As cFileEx

Parameters:

  • FilePath - File path
  • AccessMode - Access mode: "R"=Read-only, "W"=Write-only, "RW"=Read-write (default)

Example:

vb
' Read-only mode
VBMAN.FileEx.OpenFile "C:\\data.txt", "R"

' Read-write mode (default)
VBMAN.FileEx.OpenFile "C:\\data.txt"

' Chain calling
VBMAN.FileEx.OpenFile("C:\\data.txt").ReadData.ReturnText

CloseFile ​

Close file

vb
Public Sub CloseFile()

Example:

vb
VBMAN.FileEx.OpenFile "C:\\data.txt", "R"
' ... operations ...
VBMAN.FileEx.CloseFile

Buffer Settings ​

ClearBuffer ​

Clear internal buffer

vb
Public Function ClearBuffer() As cFileEx

SetBuffer ​

Fill buffer with byte array

vb
Public Function SetBuffer(Data() As Byte) As cFileEx

Example:

vb
Dim bytes(0 To 3) As Byte
bytes(0) = &H41: bytes(1) = &H42
bytes(2) = &H43: bytes(3) = &H44

VBMAN.FileEx.SetBuffer bytes

SetBufferText ​

Fill buffer with string (specify encoding)

vb
Public Function SetBufferText(ByVal Text As String, Optional ByVal CharSet As String = "UTF-8") As cFileEx

Example:

vb
VBMAN.FileEx.SetBufferText "Hello World", "UTF-8"

SetBufferBase64 ​

Fill buffer with Base64 string

vb
Public Function SetBufferBase64(ByVal Base64String As String) As cFileEx

Example:

vb
VBMAN.FileEx.SetBufferBase64 "SGVsbG8gV29ybGQ="

SetBufferHex ​

Fill buffer with Hex string

vb
Public Function SetBufferHex(ByVal HexString As String, Optional ByVal Separator As String = " ") As cFileEx

Example:

vb
VBMAN.FileEx.SetBufferHex "48 65 6C 6C 6F"
VBMAN.FileEx.SetBufferHex "48656C6C6F"

SetBufferBinString ​

Fill buffer with binary string

vb
Public Function SetBufferBinString(ByVal BinString As String, Optional ByVal Separator As String = " ") As cFileEx

Example:

vb
VBMAN.FileEx.SetBufferBinString "01001000 01100101"

AppendBuffer ​

Append bytes to buffer

vb
Public Function AppendBuffer(Data() As Byte) As cFileEx

Data Reading ​

ReadData ​

Read file data to internal buffer

vb
Public Function ReadData(Optional ByVal StartPos As Long = -1, Optional ByVal EndPos As Long = -1) As cFileEx

Parameters:

  • StartPos - Start position (1-based, -1=file beginning)
  • EndPos - End position (1-based, -1=file end)

Example:

vb
' Read entire file
VBMAN.FileEx.OpenFile("C:\\data.txt").ReadData.ReturnText

' Read specified range
VBMAN.FileEx.OpenFile "C:\\data.txt", "R"
VBMAN.FileEx.ReadData 1, 100  ' Read first 100 bytes

Data Return ​

ReturnText ​

Return buffer content as text

vb
Public Function ReturnText(Optional ByVal CharSet As String = "UTF-8") As String

Example:

vb
Dim text As String
text = VBMAN.FileEx.OpenFile("C:\\utf8.txt").ReadData.ReturnText("UTF-8")

ReturnBytes ​

Return buffer content as byte array

vb
Public Function ReturnBytes() As Byte()

Example:

vb
Dim bytes() As Byte
bytes = VBMAN.FileEx.OpenFile("C:\\data.bin").ReadData.ReturnBytes

ReturnBase64 ​

Return buffer content as Base64 string

vb
Public Function ReturnBase64() As String

Example:

vb
Dim base64 As String
base64 = VBMAN.FileEx.OpenFile("C:\\image.png").ReadData.ReturnBase64

ReturnHex ​

Return buffer content as Hex string

vb
Public Function ReturnHex(Optional ByVal Separator As String = " ") As String

Example:

vb
Dim hex As String
hex = VBMAN.FileEx.OpenFile("C:\\data.bin").ReadData.ReturnHex(" ")
' Result: 48 65 6C 6C 6F

ReturnBinString ​

Return buffer content as binary string

vb
Public Function ReturnBinString(Optional ByVal Separator As String = " ") As String

Example:

vb
Dim bin As String
bin = VBMAN.FileEx.OpenFile("C:\\data.bin").ReadData.ReturnBinString(" ")
' Result: 01001000 01100101

Data Writing ​

SaveData ​

Write buffer to file

vb
Public Function SaveData(Optional ByVal FilePath As String, Optional ByVal IsAppend As Boolean = False) As cFileEx

Parameters:

  • FilePath - Target path (default uses OpenFile path)
  • IsAppend - Whether to append (default False=overwrite)

Example:

vb
' Write new file
VBMAN.FileEx.SetBufferText("Hello World", "UTF-8").SaveData "C:\\output.txt"

' Append to file
VBMAN.FileEx.SetBufferText("New Line" & vbCrLf, "UTF-8").SaveData "C:\\log.txt", True

Comprehensive Examples ​

Example 1: Quick Text File Reading ​

vb
Private Function ReadTextFile(filePath As String, Optional charSet As String = "UTF-8") As String
    On Error GoTo ErrorHandler
    
    ReadTextFile = VBMAN.FileEx.OpenFile(filePath, "R").ReadData.ReturnText(charSet)
    VBMAN.FileEx.CloseFile
    Exit Function
    
ErrorHandler:
    ReadTextFile = ""
End Function

Private Sub TestRead()
    Dim content As String
    content = ReadTextFile("C:\\data.txt", "UTF-8")
    TextBox1.Text = content
End Sub

Example 2: Quick Text File Writing ​

vb
Private Sub WriteTextFile(filePath As String, content As String, Optional charSet As String = "UTF-8")
    On Error GoTo ErrorHandler
    
    VBMAN.FileEx.SetBufferText(content, charSet).SaveData filePath
    Exit Sub
    
ErrorHandler:
    MsgBox "Write failed: " & Err.Description
End Sub

Private Sub TestWrite()
    WriteTextFile "C:\\output.txt", TextBox1.Text, "UTF-8"
End Sub

Example 3: File Encoding Conversion ​

vb
Private Sub ConvertFileEncoding(sourcePath As String, destPath As String, sourceCharset As String, destCharset As String)
    ' Read source file
    Dim content As String
    content = VBMAN.FileEx.OpenFile(sourcePath, "R").ReadData.ReturnText(sourceCharset)
    VBMAN.FileEx.CloseFile
    
    ' Write destination file
    VBMAN.FileEx.SetBufferText(content, destCharset).SaveData destPath
    
    MsgBox "Conversion complete!"
End Sub

Private Sub TestConvert()
    ' GB2312 to UTF-8
    ConvertFileEncoding "C:\\gbk.txt", "C:\\utf8.txt", "GB2312", "UTF-8"
End Sub

Example 4: Processing Binary Files ​

vb
Private Sub ProcessBinaryFile()
    ' Read binary file
    VBMAN.FileEx.OpenFile "C:\\data.bin", "R"
    
    ' Read first 16 bytes
    Dim hexData As String
    hexData = VBMAN.FileEx.ReadData(1, 16).ReturnHex(" ")
    Debug.Print "Hex: " & hexData
    
    ' Read all and convert to Base64
    Dim base64Data As String
    base64Data = VBMAN.FileEx.ReadData.ReturnBase64
    
    VBMAN.FileEx.CloseFile
    
    ' Save Base64 to text file
    VBMAN.FileEx.SetBufferText(base64Data, "UTF-8").SaveData "C:\\base64.txt"
End Sub

Example 5: Large File Chunked Reading ​

vb
Private Sub ReadLargeFile(filePath As String)
    Const CHUNK_SIZE = 1048576  ' 1MB
    
    VBMAN.FileEx.OpenFile filePath, "R"
    
    Dim fileSize As Currency
    fileSize = VBMAN.FileEx.FileSize
    
    Dim totalRead As Currency
    totalRead = 0
    
    Do While totalRead < fileSize
        Dim startPos As Long
        Dim endPos As Long
        
        startPos = totalRead + 1
        endPos = totalRead + CHUNK_SIZE
        If endPos > fileSize Then endPos = fileSize
        
        ' Read chunk of data
        Dim chunk As String
        chunk = VBMAN.FileEx.ReadData(startPos, endPos).ReturnText("UTF-8")
        
        ' Process this chunk...
        ProcessChunk chunk
        
        totalRead = endPos
        
        ' Show progress
        Debug.Print "Progress: " & Int(totalRead / fileSize * 100) & "%"
        DoEvents
    Loop
    
    VBMAN.FileEx.CloseFile
End Sub

Example 6: Log File Appending ​

vb
Private Sub AppendLog(message As String)
    Dim logEntry As String
    logEntry = Format(Now, "yyyy-MM-dd hh:mm:ss") & " " & message & vbCrLf
    
    VBMAN.FileEx.SetBufferText(logEntry, "UTF-8").SaveData App.Path & "\\app.log", True
End Sub

Private Sub TestLog()
    AppendLog "Application started"
    AppendLog "Execute operation A"
    AppendLog "Execute operation B"
End Sub

Best Practices ​

  1. Close in Time: Call CloseFile promptly after file usage
  2. Error Handling: Add error handling mechanism for file operations
  3. Consistent Encoding: Use the same character encoding for reading and writing
  4. Chunk Large Files: Read/write large files exceeding memory capacity in chunks
  5. Path Checking: Check if file/directory exists before operation

VB6 and LOGO copyright of Microsoft Corporation