制作程序的试用版

时间:2012-06-25 06:20:13

标签: vb.net

我正在制作vb .net项目的试用版,但不计算日期,日期和时间。

请你给我任何纠正的建议。我使用以下代码

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim intTime As Integer = 1
    Dim dteLastStart, dteStartDate As Date
    Dim blnFirstTime, blnEnabled As Boolean
    Dim lngTimeLeft As Long

    blnEnabled = True
    If dteStartDate = Nothing Then
        dteStartDate = Now
    End If

    My.Application.SaveMySettingsOnExit = True


    If DateDiff(DateInterval.Day, dteLastStart, Now) < 0 Then
        'First clock change
        If intTime = 1 Then
            MsgBox("FRED has detected that you have changed your system date to an earlier date" & vbCrLf & "As FRED has built-in security," & vbCrLf & "FRED will only run until the next intTime you change your system date", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "System Date Changed")
            intTime = 2
        ElseIf intTime = 2 Then
            'Second clock change
            blnEnabled = False
            MsgBox("FRED has detected that you have changed your system date to an earlier date" & vbCrLf & "As this is the second warning, FRED will now be disabled", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "System Date Changed")
        End If
        'disables app
        If blnEnabled = False Then
            If MsgBox("FRED is disabled", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Disabled") = MsgBoxResult.Ok Then
                For Each form As Form In My.Application.OpenForms
                    form.Close()
                Next
            End If
        End If
    End If

    If DateDiff(DateInterval.Day, dteStartDate, Now) > 29 Then
        blnEnabled = False
        If blnEnabled = False Then
            If MsgBox("FRED has reached the end of it's trial.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Trial Ended") = MsgBoxResult.Ok Then
                'Close all open forms
                For Each form As Form In My.Application.OpenForms
                    form.Close()
                Next
            End If
        End If
    End If
    dteLastStart = Now
    If blnFirstTime = True Then
        blnFirstTime = False
    End If
    'Saves variable settings
    My.Settings.Save()

    lngTimeLeft = 29 - (DateDiff(DateInterval.Day, dteStartDate, Now))

    MsgBox("This is a 29-day trial version." & vbCrLf & "You have " & CStr(lngTimeLeft) & " days left.", MsgBoxStyle.OkOnly, "FRED Trial")
end sub
end class

1 个答案:

答案 0 :(得分:2)

让我们说你的节目叫做#34; MyProg&#34;并且您希望用户试用7天。

因此,从概念上讲,您将在注册表中输入:

HKLM\SOFTWARE\MyProg

每次运行软件时,您都必须检查它是否存在,如果没有,假设它是第一次运行,您将创建条目和设置值。如果存在条目,您将检索值并与现在进行比较。

否编码,这是一个处理注册表的示例函数,如果日期已过期则返回false,如果仍处于试用期,则返回true:

Private Function HandleRegistry() As Boolean
        Dim firstRunDate As Date
        firstRunDate = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\MyProg", "FirstRun", Nothing)
        If firstRunDate = Nothing Then
            firstRunDate = Now
            My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\MyProg", "FirstRun", firstRunDate)
        ElseIf (Now - firstRunDate).Days > 7 Then
            Return False
        End If
        Return True
    End Function

不是你所要做的就是调用它并处理响应:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim result As Boolean = HandleRegistry()
        If result = False Then 'something went wrong
            MsgBox("Trial expired")
        Else
            MsgBox("Trial version")
        End If
    End Sub

当然这只是示例,所以你明白了,但实用性我会编码日期并调用注册表名称输入其他东西,所以它不会是用户友好的。另外,请记住架构问题,以便了解它的编写位置。

希望有所帮助