DAT File Browser (Parsing .DAT File in Windows Form C#.Net )



In this tutorial, we’ll create a DAT File Browser using C# and DevExpress. This application will scan a directory for .dat files, parse their contents using the ByteBlocks.DatFileParser library, and display entries in a structured grid format.

Project Structure

Here is the structure of our project in the Solution Explorer:

The project includes the following key files:

  • DatEntry.cs: Defines the structure for each entry in the .dat files.
  • DatLocation.cs: Represents specific locations for .dat files.
  • PredefinedEnvironments.cs: Stores predefined locations and tags for browsing.
  • MainForm.cs: Contains the main logic for loading and displaying .dat file data.
  • Program.cs: Starts the application.

DatEntry.cs

public class DatEntry
{
    public String Type { get; set; }
    public String Object { get; set; }
    public String LastAccess { get; set; }
}


DatLocation.cs

 public class DatLocation
 {
     public String Tag { get; set; }
     public String Directory { get; set; }
 }


PredefinedEnvironments.cs

 public class PredefinedEnvironments
 {
    public static List<DatLocation> Locations = new List<DatLocation>() {
         new DatLocation() {
             Tag = "USER_FILE",
             Directory =Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
         },
         new DatLocation() {
             Tag = "COOKIES",
             Directory =Environment.GetFolderPath(Environment.SpecialFolder.Cookies),
         },
         new DatLocation() {
             Tag = "TEMP_IE",
             Directory =Environment.GetFolderPath(Environment.SpecialFolder.InternetCache),
         },
         new DatLocation() {
             Tag = "HISTORY",
             Directory =Environment.GetFolderPath(Environment.SpecialFolder.History),
         }
     };
 }


MainForm Code Walkthrough

The main form (MainForm.cs) uses DevExpress components to create a rich UI. This form inherits from FluentDesignForm, a class in DevExpress that offers an easy-to-style, modern design.

Setting Up the UI

The form has an accordion control on the left for selecting predefined directories, a toolbar with a button for browsing files, and a grid control to display the parsed data.

public partial class MainForm : DevExpress.XtraBars.FluentDesignSystem.FluentDesignForm
{
    public MainForm() {
        InitializeComponent();
    }
}

Loading Data from DAT Files

The LoadDataFiles method is responsible for scanning the specified directory for .dat files, parsing them, and binding the parsed entries to the grid control.

Step-by-Step Breakdown

    1. Begin Update and Show Loading Panel:

  • Temporarily disables the grid control updates to avoid UI flicker and show a loading indicator.
gridControl1.BeginUpdate();
gridView1.ShowLoadingPanel();

    2 Initialize Lists to Store Data:

  • urls for storing URL entries.
  • redirects for storing redirect entries.
  • entries for combining URL and redirect entries into a single list.
List<UrlEntry> urls = new List<UrlEntry>();
List<RedirectEntry> redirects = new List<RedirectEntry>();
List<DatEntry> entries = new List<DatEntry>();

    3. Retrieve and Parse DAT Files:

  • IndexDatParser.GetListOfIndexFiles gets all .dat files in the specified directory.
  • IndexDatParser.Parse parses each .dat file and fills urls and redirects based on the type of entry.
List<string> datFiles = IndexDatParser.GetListOfIndexFiles(rootFolder);
foreach (var datFile in datFiles) {
    var parseResult = IndexDatParser.Parse(datFile);
    if (parseResult.Status == 0) {
        urls.AddRange(parseResult.Entries.UrlEntries);
        redirects.AddRange(parseResult.Entries.RedirectEntries);
    }
}

    4. Transform Entries into Display Format:

  • Each UrlEntry and RedirectEntry is wrapped in a DatEntry instance with the type (URL or REDIRECT) and added to entries.
if (urls.Count > 0) {
    foreach (var urlEntry in urls) {
        var url = new DatEntry {
            Type = "URL",
            Object = urlEntry.Url,
            LastAccess = urlEntry.LastAccessTime.ToShortDateString() + " " + urlEntry.LastAccessTime.ToShortTimeString()
        };
        entries.Add(url);
    }
}

    5. Bind Entries to the Grid:

  • BindingList<DatEntry> is used to wrap entries and provide data binding to the grid.
  • Finally, gridControl1.DataSource is set to the binding source.
var bindingList = new BindingList<DatEntry>(entries);
var source = new BindingSource(bindingList, null);
gridControl1.DataSource = source;

    6. Finalize Loading and Show Data:

  • The loading panel is hidden, and updates are re-enabled for the grid.
gridView1.HideLoadingPanel();
gridControl1.EndUpdate();

Handling Accordion Item Click

The accordion control lets users navigate between predefined directories. When an item is clicked, the application loads .dat files from the associated directory,

private void AccordionItem_Click(object sender, EventArgs e) {
    AccordionControlElement item = sender as AccordionControlElement;
    if (item != null) {
        lblSelected.Caption = item.Text;
        var tag = item.Tag as string;
        if (!String.IsNullOrEmpty(tag)) {
            DatLocation location = PredefinedEnvironments.Locations.Find(x => x.Tag == tag);
            if (location != null) {
                lblDirectory.Text = location.Directory;
                LoadDataFiles(location.Directory);
            }
        }
    }
}

Browsing for a Specific DAT File

The btnBrowseFile_ItemClick method allows the user to manually select a .dat file. When the file is selected, the directory of the file is loaded.

private void btnBrowseFile_ItemClick(object sender, ItemClickEventArgs e) {
    using (OpenFileDialog fileDialog = new OpenFileDialog()) {
        fileDialog.CheckFileExists = true;
        fileDialog.CheckPathExists = true;
        fileDialog.Filter = "DAT Files (*.dat)|*.dat";
        if (fileDialog.ShowDialog() == DialogResult.OK) {
            LoadDataFiles(Path.GetDirectoryName(fileDialog.FileName));
        }
    }
}

Conclusion

This application showcases how to use DevExpress controls and a custom parser to browse and analyze .dat files. The combination of AccordionControl for navigation, GridControl for data display, and BindingList for dynamic data binding provides a powerful and user-friendly interface for handling DAT files.

With a few additions, such as more complex filtering and sorting options, you can expand this application into a fully functional browser for .dat file contents.

Previous Post Next Post

نموذج الاتصال