Je dois obtenir cette liste de journaux d'événements public static List<EventLogEntry> _LogEntries { get; private set; }
en dataGridView
dans Windows Forms.Essayer d'obtenir la liste des journaux d'événements dans un DataGridView | erreur sur la méthode ReadEventLog()
Problème actuel: Chaque fois que j'appelle la méthode
ReadEventLog()
il se casse à l'exception Une exception non gérée du type 'System.ArgumentException' a eu lieu dans System.dll à la ligneEventLog eventLog = new EventLog(EvlLocation);
D'abord, j'ouvre le fichier
// Open the log file
private void OpenFile()
{
string evlLocation = "";
// Show file open dialog
if (openFile.ShowDialog() == DialogResult.OK)
{
// Create a dataset for binding the data to the grid.
ds = new DataSet("EventLog Entries");
ds.Tables.Add("Events");
ds.Tables["Events"].Columns.Add("ComputerName");
ds.Tables["Events"].Columns.Add("EventId");
ds.Tables["Events"].Columns.Add("EventType");
ds.Tables["Events"].Columns.Add("SourceName");
ds.Tables["Events"].Columns.Add("Message");
// Start the processing as a background process
evlLocation = openFile.FileName;
parser.setLogLocation(openFile.FileName);
worker.RunWorkerAsync(openFile.FileName);
}
}
// Ensuite, la méthode ci-dessous d est appelé.
// Bind the dataset to the grid.
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
parser.ReadEventLog();
bs = new BindingSource(ds, "Events");
Foo foo1 = new Foo("TEST PC");
ComputerName.Add(foo1);
bs.DataSource = parser._LogEntries;
//Bind fooList to the dataGridView
dataGridView1.DataSource = bs;
this.Invoke(pbHandler, new object[] { 100, 100 });
}
Puis, quand ReadEventLog()
est appelé brise à la ligne EventLog eventLog = new EventLog(EvlLocation);
méthode ReadEventLog()
ci-dessous
public static void ReadEventLog()
{
// Line in question below
EventLog eventLog = new EventLog(EvlLocation);
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();
}