How to Create DAT Files

By Lysis

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. You can create these files in C# with just a few lines of code.

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\"GObinaryfile.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();

References & Resources