Simple Windows Service to find system turn on, turn off, log on, log off, lock/unlock time






4.80/5 (5 votes)
Simple Windows Service to find system turn on, turn off, log on, log off, lock/unlock time.
Introduction
This is a simple Windows Service program which writes the system turn on, turn off, log on, log off, remote on, remote off, locked, unlocked time in a text file.
Using the code
Simply create a Windows Service project using the following steps:
- In Visual Studio, click File->New->Project.
- Select Windows Service project for the project template.
- In theOnstart method, write the following code
- Right click on the service1.cs file, click View Designer.
- In designer right click and go to properties.
- Change
CanHandleSessionChange
property toTrue
. - Change
CanShutDown
property toTrue
.
protected override void OnStart(string[] args)
{
FileStream fs = new FileStream(@"c:\SystemActiveTimeInformation.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sWriter = new StreamWriter(fs);
sWriter.BaseStream.Seek(0, SeekOrigin.End);
sWriter.WriteLine("=====================================================================================");
sWriter.WriteLine("System Turn On Time: \t " + DateTime.Now);
sWriter.Flush();
sWriter.Close();
}
Add the below two method in the service1.cs file:
protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
FileStream fs = new FileStream(@"c:\SystemActiveTimeInformation.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sWriter = new StreamWriter(fs);
sWriter.BaseStream.Seek(0, SeekOrigin.End);
switch (changeDescription.Reason)
{
case SessionChangeReason.SessionLogon:
sWriter.WriteLine("System Log On Time: \t " + DateTime.Now);
break;
case SessionChangeReason.SessionLogoff:
sWriter.WriteLine("System Log Off Time: \t " + DateTime.Now);
break;
case SessionChangeReason.RemoteConnect:
sWriter.WriteLine("System Remote Connect Time: \t " + DateTime.Now);
break;
case SessionChangeReason.RemoteDisconnect:
sWriter.WriteLine("System Remote Disconnect Time: \t " + DateTime.Now);
break;
case SessionChangeReason.SessionLock:
sWriter.WriteLine("System Locked Time: \t" + DateTime.Now);
break;
case SessionChangeReason.SessionUnlock:
sWriter.WriteLine("System Unlocked Time: \t " + DateTime.Now);
break;
default:
break;
}
sWriter.Flush();
sWriter.Close();
}
protected override void OnShutdown()
{
FileStream fs = new FileStream(@"c:\SystemActiveTimeInformation.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sWriter = new StreamWriter(fs);
sWriter.BaseStream.Seek(0, SeekOrigin.End);
sWriter.WriteLine("System Turn Off Time: \n " + DateTime.Now);
sWriter.Flush();
sWriter.Close();
}
Points of Interest
CanHandleSessionChange
is a property of ServiceBase
which enables the service
to handle session change events received from a Terminal Server session. If
CanShutdown
is true, the service is notified when the system is shutting down. At shutdown, the
OnShutdown
method is called if it
has been implemented in your derived class.
History
- Created on 14-Dec-2012.