如何跟踪系统关闭原因?

时间:2016-07-15 09:30:02

标签: vb.net

我正在创建一个Windows应用程序,我需要找到系统关闭的确切原因。如何通过断电来跟踪计算机是否被关闭?系统环境中是否有任何变量告诉我们系统如何关闭?

1 个答案:

答案 0 :(得分:1)

以下是查找Kernel Power设置为BugCheckCode的所有0个事件的小例子:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim EventLogInstance As New EventLog("System") 'Open the "System" event log.

    For Each Entry As EventLogEntry In EventLogInstance.Entries 'Iterate through all entries.
        If GetEventID(Entry.InstanceId) = 41 AndAlso _
            (Entry.ReplacementStrings(0) = "0" OrElse Entry.ReplacementStrings(0).Length = 0) Then 'Checks if the "BugCheckCode" is set to 0.
            'If GetEventID() returns 41 then we know that the entry is of type "Kernel Power".

            ListView1.Items.Add(New ListViewItem(New String() {Entry.TimeGenerated.ToString("yyyy\/MM\/dd  hh:mm:ss"), Entry.Message})) 'Add the entry's date, time and error message to a ListView.
        End If
    Next
    ListView1.Sort() 'Sort the listview (here, sorting order is set to Descending).
End Sub

Private Function GetEventID(ByVal InstanceId As Long) As Long 'A function for getting the ID of an event.
    Return InstanceId And &H3FFFFFFF
End Function

<强>结果:

Result

更多信息可在以下网址找到: