Option Explicit On
Imports System.IO
Public Class cTrial
Private m_DaysUsed As Integer = 0
Private m_MaxDays As Integer = 30
Private m_Ended As Boolean = False
Private m_Filename As String = vbNullString
Public ReadOnly Property DaysUsed As Integer
Get
'Return number of days used.
Return m_DaysUsed
End Get
End Property
Public ReadOnly Property DaysLeft As Integer
Get
'Return number of days left.
Return (MaxDays - m_DaysUsed)
End Get
End Property
Public Property Filename As String
Get
Return m_Filename
End Get
Set(ByVal value As String)
'Set filename.
m_Filename = value
'Check if trial file is found.
If Not File.
Exists(Filename
) Then 'Create trial file.
If Not UpdateFile(Now.Date.AddDays(MaxDays), 0) Then
Throw New FileNotFoundException
Exit Property
End If
End If
'Check the date.
CheckDate(Now.Date)
End Set
End Property
Public ReadOnly Property HasEnded As Boolean
Get
Return m_Ended
End Get
End Property
Public Property MaxDays As Integer
Get
Return m_MaxDays
End Get
Set(ByVal value As Integer)
m_MaxDays = value
End Set
End Property
Private Sub CheckDate(ByVal Date1 As DateTime)
Dim sr As StreamReader = Nothing
Dim Date2 As DateTime = Nothing
Dim Flag As Integer = 0
Dim Tmp As String = vbNullString
Try
sr = New StreamReader(Filename)
'Read in file contents.
Tmp = XorString(sr.ReadToEnd())
'Close file.
sr.Close()
'Read trial flag.
Flag = Integer.Parse(Tmp(0))
'Read date.
Date2 = Tmp.Substring(1)
'Check if trial finished.
If (Flag <> 0) Then
m_DaysUsed = MaxDays
m_Ended = True
Else
'Return number of days used.
m_DaysUsed = MaxDays - DateDiff("d", Date1, Date2) + 1
'Check if in range.
m_Ended = (DaysUsed <= 0) Or (DaysUsed > MaxDays)
End If
Catch ex As Exception
m_DaysUsed = MaxDays
m_Ended = True
End Try
If HasEnded Then
m_DaysUsed = MaxDays
'Add date and trial end flag.
UpdateFile(Date2, 1)
End If
End Sub
Private Function UpdateFile(ByVal EndDate As DateTime, ByVal Flag As Integer) As Boolean
Dim sw As StreamWriter = Nothing
'File format is as follows flag,date
'flag 0 = still in trial
'flag 1 = trial has ended.
Try
sw = New StreamWriter(Filename)
'Write date and trial flag.
sw.Write(XorString(Flag & EndDate.Date))
'Close file.
sw.Close()
Catch ex As Exception
Return False
End Try
Return True
End Function
Private Function XorString(ByVal source As String) As String
Dim sb As New System.Text.StringBuilder()
Rnd(-1)
'Set Random seed.
Randomize(1830)
For Each c As Char In source
'Encrypt bye
Dim b As Byte = Asc(c) Xor Int(Rnd() * 256) Mod 255
'Append char to stringbuilder.
sb.Append(Chr(b))
Next c
'Return string
Return sb.ToString()
End Function
End Class