using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
using System.IO;
using System.Text;
static void Main()
{
Form1 myfrm = new Form1();
SystemEvents.SessionEnded += new ssionEndedEventHandler(myfrm.CloseEvent);
Application.Run(new Form1());
}
public void CloseEvent(object sender,SessionEndedEventArgs e)
{
FileStream fs = null;
string path;
byte[] msg ;
path = @"c:\Eran\Attendance reports\Report.txt";
fs = new FileStream(path, FileMode.Create);
msg = Encoding.ASCII.GetBytes(e.Reason.ToString());
fs.Write(msg,0,msg.Length);
if (fs != null)
{
fs.Flush();
fs.Close();
}
}
e.cancel = true;
e.cancel = false;
StackTrace Trace = new StackTrace(true);
if(Trace.FrameCount > 13)
{
if(Trace.GetFrame(14).GetMethod().Name=="WmSysCommand")
{
//The user is closing the form.
e.Cancel=true;
this.Visible=false;
}
}
Private Shared WM_QUERYENDSESSION As Integer = &H11
Private Shared systemShutdown As Boolean = False
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_QUERYENDSESSION Then
MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot")
systemShutdown = True
End If
If this is WM_QUERYENDSESSION, the closing event should be fired in the base WndProc
MyBase.WndProc(m)
End Sub WndProc
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If (systemShutdown) Then
reset the variable since they may cancel the shutdown
systemShutdown = False
If (DialogResult.Yes = _
MessageBox.Show("My application", "Would you care to save your work before logging off?", MessageBoxButtons.YesNo)) Then
e.Cancel = True
Else
e.Cancel = False
End If
End If
End Sub
Windows Management Instrumentation (WMI) is a scalable system management infrastructure that uses a single consistent, standards-based, extensible, object-oriented interface. WMI provides you with a standard way to interact with system management information and the underlying WMI APIs. WMI is used primarily by system management application developers and administrators to access and manipulate system management information.
WMI can be used to build tools that organize and manage system information so that administrators or system managers can monitor system activities more closely. For example, you can develop an application using WMI that pages an administrator when a Web server crashes.