65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.80/5 (5 votes)

Dec 14, 2012

CPOL
viewsIcon

32631

downloadIcon

1550

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:

  1. In Visual Studio, click File->New->Project.
  2. Select Windows Service project for the project template.
  3. In theOnstart method, write the following code
  4. 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();            
    }   
  5. Right click on the service1.cs file, click View Designer.
  6. In designer right click and go to properties.
  7. Change CanHandleSessionChange property to True.
  8. Change CanShutDown property to True.

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.