Posted By

tabook22 on 07/11/11


Tagged

hit VBNet aspnet counter


Versions (?)

Hit Counter


 / Published in: VB.NET
 

this code shows you how to create a hit counter, you need to create a text file first

  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2. If Not Page.IsPostBack Then
  3. Dim nCount As Int32 = 0
  4. nCount = GetCounterValue()
  5. lblTest.Text = nCount.ToString()
  6. End If
  7. End Sub
  8. Private Function GetCounterValue() As Int32
  9.  
  10. Dim ctrFile As StreamReader
  11. Dim ctrFileW As FileStream
  12. Dim sw As StreamWriter
  13. Dim strPath As String = Server.MapPath("~/app_data/indexcount.txt")
  14. Dim strCounterContents As String
  15. Dim nCounter As Int32
  16.  
  17. If (File.Exists(strPath)) Then
  18. ctrFile = File.OpenText(strPath)
  19. strCounterContents = ctrFile.ReadLine().ToString()
  20. ctrFile.Close()
  21. nCounter = Convert.ToInt32(strCounterContents)
  22. Else
  23. nCounter = 0
  24. End If
  25. nCounter += 1
  26. ctrFileW = New FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write)
  27. sw = New StreamWriter(ctrFileW)
  28. sw.WriteLine(Convert.ToString(nCounter))
  29. sw.Close()
  30. ctrFileW.Close()
  31. Return nCounter
  32. End Function

Report this snippet  

You need to login to post a comment.