1. TTfile
  2. DAT File
  3. How to create a DAT File?

How to create a DAT File?

Software programmers use DAT files to contain binary information. These files (also known as .dat files because of their file extension) hold key information, such as an application's customized settings. DAT files are written in binary, so the saved information is on the user's hard drive but cannot be read. This gives the developer a way to save information into files without the worry of a user changing it and causing bugs in the application.

Create DAT files in C#

Step 1

Create a filestream variable and create the DAT file. The filestream object is a part of C# that allows you to create, open, delete and edit files. This object is beneficial for any application that writes files to a hard drive. Here is the code to create the object:

FileStream myFile = File.Create(Server.MapPath("myFile.dat"));

Step 2

Open a binary stream. This object is also included in the C# library resources. The binary object allows the programmer to write pure binary code to a file, so users are not able to read the data within the file. The following code opens the file created in step 1 for binary use:

BinaryWriter binaryfile = new BinaryWriter(myFile);

Step 3

Create a string, which you will later save to the DAT file. In this example, only one string is written to the file, but you can choose to have several strings saved to the file. Here is the code that creates a string, and then writes it to the binary file:

string myString = "My First Binary File";

binaryfile.Write(myString);

Step 4

Close the file object and the binary file object. Closing the file saves it to the hard drive. If not saved, the file cannot be accessed by other users or applications. Here is the code to close both file objects:

binaryfile.Close();

myFile.Close();

Create DAT files in VB

DAT files can also be created with VB code and display area.

You just need to put two commandbuttons and a picture box on the form and add the following code:

0

Private Type ego

num As Integer

name As String * 40

End Type

1

Dim egg As Integer, reccount As Integer

Private Sub Command1_Click()

Dim menuu As ego

egg = Len(menuu)

Open "d:\Test.dat" For Random As #4 Len = egg

reccount = LOF(4) / egg

reccount = reccount + 1

menuu.num = 1

menuu.name = "c:\my documents\my pictures\begin.gif"

Put #4, reccount, menuu

Close #4

End Sub

2

Private Sub Command2_Click()

Dim hunse As ego

egg = Len(hunse)

Open "d:\Test.dat" For Random As #5 Len = egg

reccount = 1

Get #5, 1, hunse

Picture1.Picture = LoadPicture(hunse.name)

Print hunse.num

Close #5

End Sub