2017-02-13 13 views
1

Я пытаюсь разобрать файл журнала событий в dataGridView в Windows Forms. Я использую класс EventLogEntry для этого, но столкнулся с проблемой. Мне нужно получить анализируемый журнал, который я привел в список, из метода, в котором он находится, и помещен как dataSource для dataGridView в программу.Использование списка из метода в формах Windows

Мне нужно использовать список `_LogEntries' вне метода его содержится. Я не знаю, как это сделать

public static class parser 
{ 
    public static string evlLocation = "%Program Files (x86)%\\EventLogParser\\ImportedEventLogs\\" + varBank.logInput; 
    public static string evlLocationManual = "K:\\Event Log\\Test\\BSN_Navigator.evt"; 

    public static void ReadEventLog() 
    { 
     EventLog eventLog = new EventLog(evlLocationManual); 
     EventLogEntryCollection eventLogEntries = eventLog.Entries; 
     int eventLogEntryCount = eventLogEntries.Count; 
     for (int i = 0; i < eventLogEntries.Count; i++) 
     { 
      EventLogEntry entry = eventLog.Entries[i]; 
      //Do Some processing on the entry 
     } 
     List<EventLogEntry> _LogEntries = eventLogEntries.Cast<EventLogEntry>().ToList(); 
    } 
} 

Ниже приведен код из MainForm.cs для Windows Forms

private List<Foo> ComputerName = new List<Foo>(); 
    private List<Foo> EventId = new List<Foo>(); 
    private List<Foo> EventType = new List<Foo>(); 
    private List<Foo> SourceName = new List<Foo>(); 
    private List<Foo> Message = new List<Foo>(); 

    class Foo : INotifyPropertyChanged 
    { 
     private string bar_; 
     public string Bar 
     { 
      get { return bar_; } 
      set 
      { 
       bar_ = value; 
       NotifyPropertyChanged("Bar"); 
      } 
     } 

     public Foo(string bar) 
     { 
      this.Bar = bar; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void NotifyPropertyChanged(string info) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(info)); 
      } 
     } 

     public override string ToString() 
     { 
      return bar_; 
     } 
    } 

    // Bind the dataset to the grid. 
    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
     var bs = new BindingSource(ds, "Events"); 
     Foo foo1 = new Foo("someStuff"); 
     ComputerName.Add(foo1); 

     bs.DataSource = parser._LogEntries; 
     //Bind fooList to the dataGridView 
     dataGridView1.DataSource = bs; 
     //I can see bar1 in the listbox as expected 

     this.Invoke(pbHandler, new object[] { 100, 100 }); 
    } 
+0

Вы столкнулись с проблемой ? Пожалуйста, дополните. С какой проблемой вы столкнулись? – CodingYoshi

+0

Мне нужно использовать List '_LogEntries 'вне метода, в котором он содержится. Я не знаю, как это сделать. –

ответ

1

В комментарии вы сказали:

мне нужно использовать список `_LogEntries' вне метода он содержится в

Вам просто нужно свойство в классе, как показано ниже:.

public static class parser 
{ 
    public static string evlLocation = "%Program Files (x86)%\\EventLogParser\\ImportedEventLogs\\" + varBank.logInput; 
    public static string evlLocationManual = "K:\\Event Log\\Test\\BSN_Navigator.evt"; 

    public static void ReadEventLog() 
    { 
     EventLog eventLog = new EventLog(evlLocationManual); 
     EventLogEntryCollection eventLogEntries = eventLog.Entries; 
     int eventLogEntryCount = eventLogEntries.Count; 
     for (int i = 0; i < eventLogEntries.Count; i++) 
     { 
      EventLogEntry entry = eventLog.Entries[i]; 
      //Do Some processing on the entry 
     } 
     LogEntries = eventLogEntries.Cast<EventLogEntry>().ToList(); 
    } 

    public static List<EventLogEntry> LogEntries {get; private set;} 
}