//Place this code in Form_Load event Microsoft.Win32.SystemEvents.SessionEnding += new Microsoft.Win32.SessionEndingEventHandler(SystemEvents_SessionEnding);// Add this eventhandler in the main form of your applicationvoid SystemEvents_SessionEnding(object sender, Microsoft.Win32.SessionEndingEventArgs e){ MessageBox.Show("Please close the application and then logout or shutdown!"); e.Cancel = true;}