30 Day Trial Example Update
Hi this is a simple bit of code to allow you to add 30 day trial to your programs.
Hope you may find it usfull.
Comments and suggestions welcome.
Hi this is a new update the code now been converted to a class with a few fixes to the code.
class code
vbnet Code:
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
Example code put into a new form.
vbnet Code:
Option Explicit On
Imports System.IO
Public Class frmmain
Private MyTrial As New cTrial()
Private Sub ShowMsg(ByVal msg As String)
MessageBox.Show(msg, "Trial-Demo",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub frmmain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With MyTrial
'Set the number of trial days.
.MaxDays = 30
'Tril file, hide this file somewere were no one can find it.
.Filename = Path.Combine(Application.StartupPath, "trial.sys")
'Check if trial has ended.
If .HasEnded Then
ShowMsg("Your " & .MaxDays & " day trial has eneded.")
Else
ShowMsg("You are on day " & .DaysUsed & " of " & .MaxDays & " days.")
End If
End With
End Sub
End Class
Re: 30 Day Trial Example Update
Looks like, without me testing, all someone has to do to keep using it is set back the clock on their computer. I had a trial version of Toad that I used for years that way :D
Re: 30 Day Trial Example Update
Quote:
Looks like, without me testing, all someone has to do to keep using it is set back the clock on their computer. I had a trial version of Toad that I used for years that way
Yep it's basic tho the date is encrypted so they need to decrypt first plus you can hide the file somewere hard to find.
I am going to think of something that if the date is set back, just need to think of something.
Re: 30 Day Trial Example Update
How to add this? I understand that I have to add new class but how about that " example code put into form" ?:/
Re: 30 Day Trial Example Update
Hey thanks for sharing such a wonderfull code, i learned alot from it i just have a question how does decryption works? ( i understood encryption using XOR gate)
thanks for your time.