Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.80/5 (5 votes)
11 Mar 2013CPOL 31.8K   1.5K   12   4
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. C#
    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:

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSimple Windows Service to find system turn on, turn off, log on, log off, lock/unlock time Pin
Member 1095096015-Aug-14 0:00
Member 1095096015-Aug-14 0:00 
QuestionService Not Running once the System Password Expires Pin
Member 341188919-Nov-13 20:25
Member 341188919-Nov-13 20:25 
AnswerRe: Service Not Running once the System Password Expires Pin
Rajasakthimaridasan27-Feb-14 20:23
Rajasakthimaridasan27-Feb-14 20:23 
QuestionQuestions... Pin
Cristian Rondón28-Aug-13 10:41
Cristian Rondón28-Aug-13 10:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.