简体   繁体   中英

Saving and reading a file into a data table - C#

I am new to this so bear with me. I am trying to create an application that can open a file, load it and then populate the data into a table. I have managed to hardcode it to the test file I wanted but now need to be able to open any file of the same extension.

The code I have so far is included. Appreciate if someone could point me in the right direction :) Thanks, Jo

   OpenFileDialog ofd = new OpenFileDialog();

   private void Button3_Click(object sender, EventArgs e)
   {
       ofd.Filter = "evtx|*.evtx"; //Only allows evtx file types to be seen and opened 
       if (ofd.ShowDialog() == DialogResult.OK) //Opens the file dialog on button click
       {
           this.fileNameTextBox.Text = ofd.FileName;
           saveFileNameTextBox.Text = ofd.SafeFileName;                
       }
   }

   private void loadFileButton_Click(object sender, EventArgs e)
   {
       var dt = new DataTable();
       dt.Columns.Add("Level");
       dt.Columns.Add("Logname");
       dt.Columns.Add("Event ID");
       dt.Columns.Add("Date and Time");

       using (var reader = new EventLogReader(@"C:\Users\Jason\Desktop\Event logs\Security.evtx", PathType.FilePath))
       {
           EventRecord record;
           while ((record = reader.ReadEvent()) != null)
           {
               using (record)
               {                       
                   dt.Rows.Add(record.Level, record.LogName, record.RecordId, record.TimeCreated.Value.ToString("dd/MM/yyyy tt:hh:mm:ss"));              
               }
           }
       }

       tblLogViewer.DataSource = dt;
   }

If I understand what your issue is, you are prompting for a file in Button3_Click() with...

if (ofd.ShowDialog() == DialogResult.OK) //Opens the file dialog on button click
{
    this.fileNameTextBox.Text = ofd.FileName;
    saveFileNameTextBox.Text = ofd.SafeFileName;                
}

...but then in loadFileButton_Click() you are using a different path to construct an EventLogReader ...

using (var reader = new EventLogReader(@"C:\Users\Jason\Desktop\Event logs\Security.evtx", PathType.FilePath))
{

You are already saving the selected file's path to fileNameTextBox.Text , so just pass that property to the EventLogReader constructor instead...

using (var reader = new EventLogReader(fileNameTextBox.Text, PathType.FilePath))
{

Note that loadFileButton_Click assumes that ofd has previously been displayed and accepted (not canceled). Without knowing what your different buttons are, it might be better to create and use your EventLogReader immediately after successfully prompting for an input file...

if (ofd.ShowDialog() == DialogResult.OK) //Opens the file dialog on button click
{
    this.fileNameTextBox.Text = ofd.FileName;
    saveFileNameTextBox.Text = ofd.SafeFileName;                

    using (var reader = new EventLogReader(ofd.FileName, PathType.FilePath))
    {
        // Use reader...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM