Regarding Windows Service.

Hi all, i donot know this is the right place to ask this question. ok

my problem is i want to track the logon and logoff on a machine using the .net windows service. do anyone of you worked on this type, then please help me.

Thankx.




Answer this question

Regarding Windows Service.

  • Janni Kajbrink

    But there is a major problem . using windows sevice i can get the last logon time once when windows started. but how can i track each time the user goes to logoff and logon without shutting down the computer.

  • MrT25

    im not sure about this part really. that indicates that the last logoff time is unknown. I'll see what else I can dig up but sure other people will also chip in!

  • Vicam

    ok first thank for your reply. and i am going to use it. i inform you the result.

  • Igor Grozman

    thanks Ahmed, Your code help is getting me the last logon time. but it will not show the last logoff time. the last logoff time shown as (***********.**+*** ) this format. do you give some idea regarding this.

  • Chris_CG

    actually i want my own service to do this. so that i can record the login and logoff time of the machine.

  • DaveMcL

    I think you can do this using WMI. The Environment.HasShutDownStarted property will indicate of the CLR is unloading/shutting down or perhaps look at the OnStop event of the service to log your time or something. in WMI:

    add a reference to System.Management. Import the System.Management and System.Management.Instrumentation namespaces then try this:


    Dim theScope as new ManagementScope("\\" & Environment.MachineName & "\root\cimv2")
    Dim theQueryBuilder as new StringBuilder()
    theQueryBuilder.Append(
    "SELECT LastBootUpTime FROM Win32_OperatingSystem")
    Dim theQuery as new ObjectQuery(theQueryBuilder.ToString())
    Dim theSearcher as new ManagementObjectSearcher(theScope, theQuery)
    Dim theCollectionOfResults as ManagementObjectCollection = theSearcher.Get()
    for each theCurrentObject as ManagementObject in theCollectionOfResults

    Dim result as string = theCurrentObject("LastBootUpTime").ToString()
    Dim theTicks = new DateTime(Convert.ToInt32(result.Substring(8, 6)))
    Dim t as new DateTime(Convert.ToInt32(result.Substring(0, 4)), Convert.ToInt32(result.Substring(4, 2)), Convert.ToInt32(result.Substring(6, 2)))
    t = t.AddTicks(theTicks.Ticks)
    TimeSpan diff = DateTime.Now - t
    MessageBox.Show(diff.ToString())

    Next


    Remember Windows Service doesnt have MessageBoxes, these are here just for your convenience if you are testing it out on a WinForms app first

    this is not the best code, since you are doing string parsing etc... and can have culture issues but this is the way WMI brings back the time since bootup:

    20061027180725.375000+060

    for example. I'm not sure if this would be the login time, unlikely.

    Maybe take a look at the Win32_NetworkLoginProfile class and the LastLogon time property.



    Dim searcher As New ManagementObjectSearcher( _
    "root\CIMV2", _
    "SELECT * FROM Win32_NetworkLoginProfile")
    For Each queryObj As ManagementObject in searcher.Get()
    Console.WriteLine("-----------------------------------")
    Console.WriteLine("Win32_NetworkLoginProfile instance")
    Console.WriteLine("-----------------------------------")
    Console.WriteLine("LastLogoff: {0}", queryObj("LastLogoff"))
    Console.WriteLine("LastLogon: {0}", queryObj("LastLogon"))
    Console.WriteLine("LogonHours: {0}", queryObj("LogonHours"))
    Console.WriteLine("Name: {0}", queryObj("Name"))
    Console.WriteLine("Profile: {0}", queryObj("Profile"))
    Console.WriteLine("UserId: {0}", queryObj("UserId"))
    Console.WriteLine("UserType: {0}", queryObj("UserType"))
    Next



  • regthesk8r

    You can turn on auditing so logon/logoff gets tracked in the security eventlog. If that is enough:

    Start->Administrative Tools->Local Security Policy : Security Settings -> Local Policies -> Audit Policy : Audit Logon events : enable success and failure audit.

    --
    SvenC


  • j_o_h_a_n_n_e_s

    Ok i also try to get it. Thankx again.

  • Regarding Windows Service.