Click here to Skip to main content
Click here to Skip to main content

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

By , 11 Mar 2013
Rate this:
Please Sign up or sign in to vote.

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.

License

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

About the Author

Rajasakthimaridasan
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
QuestionService Not Running once the System Password Expires PinmemberMember 341188919-Nov-13 20:25 
AnswerRe: Service Not Running once the System Password Expires PinmemberRajasakthimaridasan27-Feb-14 20:23 
QuestionQuestions... PinmemberCristian Rondón28-Aug-13 10:41 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140421.2 | Last Updated 11 Mar 2013
Article Copyright 2012 by Rajasakthimaridasan
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid