I want to store events from other applicati开发者_Go百科ons in my own app. Examples of events: when Word is opened, minimized or a file is opened.
Is such a thing possible?
Running a program and opening a file are OS events (they involve security checks), and can be captured using the Windows built-in auditing features. Those are off-topic here, direct further questions to ServerFault.com
Minimizing a program is an example of an application message, to get those you would need to install a hook using SetWindowsHookEx
. Beginning in .NET 4 (which introduces parallel runtime support), this can be done with C#, but it's not recommended. One huge issue you need to be aware of is that your event capture code must NEVER generate events of its own, or else you will start a chain reaction that crashes all running programs.
Use EventLog
.
You must set EnableRaisingEvents
property to true
And When an event add to specified event log the
EntryWritten
event handler will raise
It is the simplest way to handle os events
sample code
private void frmMain_Load(object sender, EventArgs e)
{
System.Diagnostics.EventLog s = new System.Diagnostics.EventLog("Application", ".", "");
s.EnableRaisingEvents = true;
s.EntryWritten += delegate(object st, System.Diagnostics.EntryWrittenEventArgs ew)
{
MessageBox.Show(ew.Entry.Message);
};
}
精彩评论