0

I am trying to read an event log file, I am able to send the file into the code, however for the file I am trying to read, located "C:\Users\banvilb\Documents\Event Log\Test\BSN_Navigator.evt", is bringing up "Invalid File Format"

The if statement highlighted in the code snippet below is what is bringing up the message, but I do not understand why the message is appearing.

What am I doing that is causing it not to read the file.

If you need more/the rest of the code tell me and I will update this post

    // Parse the file
    public unsafe void Parse(string filename)
    {
        try
        {
            // Open the file
            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                // Use BinaryReader to read the file
                using (BinaryReader br = new BinaryReader(fs))
                {
                    //Read the header of the file
                    byte[] header = new byte[sizeof(EventLogHeader)];
                    br.Read(header, 0, header.Length);
                    EventLogHeader _h = new EventLogHeader(header);
                    // Validate the file

                    // **** The issue is here ****
                    if (!Validate(_h))
                    {
                        this.OnAction("Invalid file format.");
                        return;
                    }

                    //
                    int totalEvents = (int)(_h.NextIndex - 1);
                    this.OnAction(String.Format("Found {0} events", totalEvents));
                    // Read the items
                    EventLogEntry e;
                    int cnt = 0;
                    uint offset = _h.FooterOffset;
                    while (true)
                    {
                        byte[] buff = ReadEntry(br, ref offset);
                        e = ReadEntry(buff);
                        cnt++;
                        DateTime dt = GetTime(e.rec.TimeGenerated);
                        this.OnFoundRecord(
                            new object[] { 
                                Enum.GetName(typeof(EventLogEntryType),e.rec.EventType),
                                dt.ToShortDateString(),
                                dt.ToShortTimeString(),
                                e.SourceName,
                                e.Strings,
                                e.rec.EventCategory,
                                e.rec.EventID,
                                e.UserSid, 
                                e.Computername});
                        if (cnt % 200 == 0) this.OnProgress(cnt, totalEvents);
                        if (offset == 48)
                            break;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            this.OnAction(String.Format("Error Occured! {0}", ex.Message));
        }
        return;
    }
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • What is happening on Validate(byte[]) method?. Also, It will help you A LOT if you watch the contents of 'header' variable when Validate method returns false. – celerno Oct 21 '16 at 21:05
  • Comment out yellow box. Your file doesn't pass required header format. – Xaqron Oct 21 '16 at 21:05
  • 1
    We don't have the code for `Validate()`. How are we supposed to know what it does and why it thinks the file is the wrong format? – Ken White Oct 21 '16 at 21:07

0 Answers0