In this article I will explain with an example, how to read DAT file in Windows (WinForms) Application using C# and VB.Net.
 
 

Form Design

The following Form consists of:
DataGridView – For displaying DAT file content.
OpenFileDialog – For selecting DAT file.
Read DAT file in C# and VB.Net
 
 

Namespaces

You will need to import the following namespace.
C#
using System.IO;
 
VB.Net
Imports System.IO
 
 

Property class

The Customer class consists of the following properties.
C#
public class Customer
{
    public string CustomerId { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}
 
VB.Net
Public Class Customer
    Public Property CustomerId As String
    Public Property Name As String
    Public Property Country As String
End Class
 
 

Reading DAT file

Inside the Form Load event handler, the ReadDatFile method is called.

ReadDatFile

Inside the ReadDatFile method, the InitialDirectory property is set.
The Filter property is set to Dat files(*.Dat)|*.Dat as file with only .DAT extension is required and RestoreDirectory is set to TRUE.
The RestoreDirctory property of the OpenFileDialog is set to TRUE which stores the previously selected directory that has been set using InitialDirectory.
Then, a check is performed if the file is selected or not.
After that the selected file content is read using ReadAllLines method of File class and stored in an array of string.
A Generic List collection of Customer class is created and a FOR loop is executed over the array of string which has contents read from selected file.
Each line is splits using tab (\t) character and stored in an array using named customer.
A new object of Customer class is created and read values are set to their respective properties.
Finally, a Generic List collection of Customer class object is assigned to the DataSource property of DataGridView and the DataGridView is populated.
C#
private void Form1_Load(object sender, EventArgs e)
{
    this.ReadDatFile();
}
 
private void ReadDatFile()
{
    openFileDialog1.InitialDirectory = @"E:\Files";
    openFileDialog1.Filter = "Dat files(*.Dat)|*.Dat";
    openFileDialog1.RestoreDirectory = true;
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        string[] lines = File.ReadAllLines(openFileDialog1.FileName);
        List<Customer> customers = new List<Customer>();
        for (int i = 0; i <= lines.Length - 1; i++)
        {
            if (!string.IsNullOrEmpty(lines[i].Trim()))
            {
                string[] customer = lines[i].Trim().Split('\t');
                customers.Add(new Customer
                {
                    CustomerId = customer[0],
                    Name = customer[1],
                    Country = customer[2]
                });
            }
        }
        dataGridView1.DataSource = customers;
    }
}
 
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.ReadDatFile()
End Sub
 
Private Sub ReadDatFile()
    OpenFileDialog1.InitialDirectory = "E:\Files"
    OpenFileDialog1.Filter = "Dat files(*.Dat)|*.Dat"
    OpenFileDialog1.RestoreDirectory = True
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
        Dim lines As String() = File.ReadAllLines(OpenFileDialog1.FileName)
        Dim customers As List(Of Customer) = New List(Of Customer)()
        For i As Integer = 0 To lines.Length - 1
            If Not String.IsNullOrEmpty(lines(i).Trim()) Then
                Dim customer As String() = lines(i).Trim().Split(vbTab)
                customers.Add(New Customer With {
                .CustomerId = customer(0),
                .Name = customer(1),
                .Country = customer(2)
            })
            End If
        Next
        DataGridView1.DataSource = customers
    End If
End Sub
 
 

Screenshots

DAT File

Read DAT file in C# and VB.Net
 

DataGridView

Read DAT file in C# and VB.Net
 
 

Downloads