Search code examples
vb.netregistrylicensing

Delete registry after year Vb net


How can I make my licence works only one year? When I activated my program I add registry with current date. On each run I check if the registry exist. If registry exists I can use my application otherwise it prompt me to activate it .

I want to make that my licence works only one year.

I tried with

    Public Sub Licence_Check()


    Dim licenca As Date
            Dim days As Integer
            licenca = CDate(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\tFull", "tFull", Nothing))
        days = CInt(365 - (DateDiff(DateInterval.Day, licenca, Now)))

        If days <= 0 Then
            My.Computer.Registry.CurrentUser.DeleteValue("tFull")
            pbInfo.Value = pbInfo.Maximum
            gpbInfo.Text = "One year Licence OVER"
            RbContinue.Enabled = False
        End If
End Sub

Solution

  • I have already warned you about the weakness of this approach, but this is how you could write the above License_Check

    Public Sub Licence_Check()
        Dim licenca As Date
        Using tempKey = My.Computer.Registry.CurrentUser.OpenSubKey("Software\YourAppNameHere", true)
            licenca = CDate(tempKey.GetValue("InstallDate", Nothing))
            Dim limit = licenca.AddYears(1)
            if limit < DateTime.Today
                tempKey.DeleteValue("InstallDate")
                Console.WriteLine ("One year Licence OVER")
            End If
       End Using
    End Sub
    

    Notice that you should store your application info in a subkey of the SOFTWARE subkey and not directly under the root HKEY_CURRENT_USER. Then you need to open the appropriate subkey asking for the write permissions and finally read the value desidered.

    To check the dates just add 1 year to the value read and do the check against Today. To delete you should use the DeleteValue method of the opened subkey.

    Also I suppose that this is not a check that should be applied on a user by user base. Instead it is a check that could be applied to the whole application. In this case you should use the predefined My.Computer.Registry.LocalMachine registry key.