如何计算应用点击次数

时间:2014-08-13 03:12:25

标签: .net vb.net visual-studio odbc

需要应用程序启动器帮助,

我想跟踪用户为启动程序而进行的每次点击,并将信息收集到数据库中,(txt / odbc)以便日后查看或输出到其他程序

申请名称: 点击次数:

以下是我的应用程序启动程序用于启动新程序的代码:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    System.Diagnostics.Process.Start("C:\notepad.exe")
    Me.Close()
End Sub

我是编程新手。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

有一个这样的子

Private Sub WriteData(_text As String, _where As String)
    Dim w As New System.IO.StreamWriter(_where)
    w.Write(_text)
    w.Close()
End Sub

和像

这样的全局变量
Private Counter as Integer = 0

您可以将一些数据保存到文本文件中。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    System.Diagnostics.Process.Start("C:\notepad.exe")
    Counter += 1
    WriteData(Counter.ToString, "C:\TEMP.TXT")
    Me.Close()
End Sub

<强>更新

您需要将以下行添加到页面顶部(以便能够使用StreamWriter

Imports System.IO

更新2

我做了一些像我们的评论。请尝试下面的代码:

Public Class Form1

    Private Counter As New Dictionary(Of String, Integer)
    Private _whereDirectory As String = "C:\TEMP\"
    Private _where As String = _whereDirectory & "LOG.TXT"

    Private Sub WriteData()
        If Not IO.Directory.Exists(_whereDirectory) Then
            IO.Directory.CreateDirectory(_whereDirectory)
        End If
        'Write data when something is clicked
        Dim w As New System.IO.StreamWriter(_where)
        For Each item In Counter
            w.WriteLine(item.Key & ":" & item.Value)
        Next
        w.Close()
    End Sub

    Sub ReadData()
        'Read the log when the program starts
        Dim r As New System.IO.StreamReader(_where)
        Dim line As String = r.ReadLine()
        While Not line Is Nothing
            Dim items() As String = line.Replace(" ", "").Split(":")
            If items.Count = 2 AndAlso IsNumeric(items(1)) Then
                Try
                    Counter.Add(items(0), CInt(items(1)))
                Catch ex As Exception

                End Try
            End If
            line = r.ReadLine
        End While
        r.Close()
    End Sub

    Sub UserClicked(_what As String)
        'Increment the counter
        If Counter.ContainsKey(_what) Then
            Counter(_what) += 1
        Else
            Counter.Add(_what, 1)
        End If
    End Sub

    Private Sub btNotepad_Click(sender As System.Object, e As System.EventArgs) Handles btNotepad.Click
        'Clicking on notepad button
        System.Diagnostics.Process.Start("Notepad.exe")
        UserClicked("Notepad")
        WriteData()
        Me.Close()
    End Sub

    Private Sub btIE_Click(sender As System.Object, e As System.EventArgs) Handles btIE.Click
        'Clicking on IE button
        System.Diagnostics.Process.Start("iexplore", "http://www.google.com")
        UserClicked("IE")
        WriteData()
        Me.Close()
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ReadData()
    End Sub
End Class

结果是一个文本文件,如下面的文字:

Notepad:2 
IE:2