It's quite simple:
Add "Exit" property to the application tag
<Application x:Class="WpfApplication4.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Exit="Application_Exit">
</Application>
and handle it in the "code behind"
private void Application_Exit(object sender, ExitEventArgs e)
{
// Perform tasks at application exit
}
The Exit event is fired when the application is shutting down or the Windows session is ending. It is fired after the SessionEnding event. You cannot cancel the Exit event.