22/11/2011

How to monitor user activities of my software? VB.Net code for beginners

A user activities like who at what time logged in and logged out from your application can be monitored by creating a log file which catch the user id and log in time when ever a user logged in by entering his/her user id and password in log in form. As ethically it is not advice able to catch the user id and password  both together so in my post I will show only how to monitored the user the user activities like log in time and logged out time in my application.For better understanding of these type of vb.net code or database related inquiry the book written by Mike Murach is very good book which is available on amazon with free shipping charge, I will suggest one can  refer them. Beside these book if you want a software to monitor all activities of your computer user then  Max keylogger is an all in one keystroke logger & monitoring Software. it is a complete PC surveillance software package, designed to keep track of all keystrokes. In addition to its primary key capture function it performs as a full-fledged computer monitoring Software: This unique software allows remote computer monitoring and keylogger recording in real time such as logs any password and makes screenshots, tracks Internet sessions etc. 

Let discuss the VB.net code for recording user activities.I have designed my log in form like this


In code side I have created a a function for catching the user ID  with timing whenever a user enter his user id and password and clicks OK button. The Visual Basic .Net code for my function is 

Private Sub log()
        Try
            Dim s As StreamWriter
            Dim portfolioPath As String = My.Application.Info.DirectoryPath
            If Not Directory.Exists("C:\Program Files\ABC\ABC") Then
      Directory.CreateDirectory("C:\Program Files\ABC\ABC")
      File.Create("C:\Program Files\ABC\ABC\log.rtf").Close()
     s = New StreamWriter("C:\Program Files\ABC\ABC\log.rtf", True)
    s.WriteLine("User Id login " & TxtUserId.Text & "-" & Date.Now)
    s.Flush()
    s.Close()
    Else
    s = New StreamWriter("C:\Program Files\ABC\ABC\log.rtf", True)
    s.WriteLine("User Id login " & TxtUserId.Text & "-" & Date.Now)
    s.Flush()
    s.Close()
    End If
    Catch ex As Exception
   Dim MessageString As String = "Report this error to the system administrator: " & ControlChars.NewLine & ex.Message
Dim TitleString As String = "Employee Master Details Data Load Failed"
MessageBox.Show(MessageString, TitleString, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
    End Sub

And I call this function on OK or WithoutID button click event whenever a user after entering his ID and password . My code on click button is

    Private Sub BtnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOk.Click
        Try
            If Counter = 8 Then
                MsgBox("You have exceeded the number of attempts")
                FrmSplash1.Close()
                Me.Close()
                Exit Sub
            End If
            If TxtUserId.Text = "" Or TxtPassword.Text = "" Then
                MsgBox("You must enter a user name and password.", _
                vbOKOnly + vbInformation)
                TxtUserId.Focus()
      ElseIf TxtUserId.Text = "abcd" And TxtPassword.Text = "abcd@1234" Then
                FrmMainMDI.Show()
                Me.Hide()
                Me.log()

            Else

                Dim CmdER As New OleDbCommand
                Dim MyReader As OleDb.OleDbDataReader
                CmdER.Connection = cn
                CmdER.CommandText = "Select * from UserAccount where UserId = '" & TxtUserId.Text & " 'and UserPassword =  '" & TxtPassword.Text & "'"
                MyReader = CmdER.ExecuteReader
                If MyReader.Read() Then
                    TxtUserAccout.Text = MyReader("AccountType")
                      'txtUserAccouttype.text is hidden not visible in the log in form
                    If TxtUserAccout.Text = "A" Then
                        FrmMainMDI.Show()
                        Me.Hide()
                FrmMainMDI.DatabackUpToolStripMenuItem.Enabled = False
                        Me.log()
                    Else
                        FrmMainMDI.Show()
                        Me.Hide()
                        FrmMainMDI.UtilityToolStripMenuItem.Enabled = False
                        Me.log()
                    End If
                    LoginSucceeded = True
                ElseIf MsgBox("Wrong user name or password") Then
                    TxtUserId.Text = ""
                    TxtPassword.Text = ""
                    TxtPassword.Focus()
                    Counter = Counter + 1
                    If MsgBox("Invalid Password, try again!", , "Login") Then
                        TxtUserId.Text = ""
                        TxtPassword.Text = ""
                        TxtUserId.Focus()
                        Counter = Counter + 1
                    End If
                End If
            End If
        Catch ex As Exception
            Dim MessageString As String = "Report this error to the system administrator: " & ControlChars.NewLine & ex.Message
            Dim TitleString As String = "Employee Master Details Data Load Failed"
            MessageBox.Show(MessageString, TitleString, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
To understand Vb.Net in better way I will suggest you to have  book written by Anne Prince  "Murach Visual Basic" and that is available on  Amozon and Flipkart. for Keyloger software click here