Read and Write Binary Files in VB6, VBScript, ASP

The Chilkat FileAccess ActiveX is a freeware component that may be used to read and write binary files in VBScript, ASP, VB6, etc. Here are some sample code fragments:


' Download the Chilkat FileAccess ActiveX (freeware) from here:
set fac = CreateObject("Chilkat.FileAccess")

' Read an entire file as binary data (returns Variant containing byte array)
byteData = fac.ReadEntireFile("dude.gif")

' Save bytes to a file:
success = fac.WriteEntireFile("out.gif", byteData)
if (success = 0) then
    MsgBox "Failed to write file."
end if



' Download the Chilkat FileAccess ActiveX (freeware) from here:
set fac = Server.CreateObject("Chilkat.FileAccess")

' Read the entire binary file into a Variant containing a byte array
fileData = fac.ReadEntireFile(Server.MapPath("data/dude.gif"))

' Write the entire byte array to a file:
success = fac.WriteEntireFile(Server.MapPath("data/out.gif"), fileData)
if (success = 0) then
    Response.Write "Failed."
    Response.Write "Success."
end if


Visual Basic 6.0

' Download the Chilkat FileAccess ActiveX (freeware) from here:

' Add a reference to Chilkat FileAccess
Dim fac As New CkFileAccess

Dim fileData() As Byte
fileData = fac.ReadEntireFile("dude.gif")

Dim success As Long
success = fac.WriteEntireFile("out.gif", fileData)
If (success = 0) Then
    MsgBox "Failed to write file."
End If

Here are some example for reading/writing binary files in C#, VB.NET, and C/C++:


FILE *fp = fopen("dude.gif","rb");
// How big is the file?
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
unsigned char *fileData = new unsigned char[size];
// Position to the start of the file:

fp = fopen("out.gif","wb");


byte[] fileData = System.IO.File.ReadAllBytes("dude.gif");
System.IO.File.WriteAllBytes("out.gif", fileData);


Dim fileData As Byte()
fileData = System.IO.File.ReadAllBytes("dude.gif")
System.IO.File.WriteAllBytes("out.gif", fileData)