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.
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.
Regarding Windows Service.
Janni Kajbrink
MrT25
Vicam
Igor Grozman
Chris_CG
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