Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Implementing a Rudimentary Count Based Trial Version Plugin for Windows Applications

4.13/5 (8 votes)
23 Apr 2012CPOL2 min read 25.6K   869  
Implementing a Rudimentary Count Based Trial Version Plugin for Windows Applications

Introduction

This tip describes a very rudimentary approach to implement a trial version of an application that will open for some predefined number of times.

Background

Many a times, we want to demonstrate our application with full functionality to someone. While doing so, there is a risk of that application being used at the other end. This small tip describes a very quick way of implementing a Trial version in an application so that the application can only be opened for some limited number of times.

The approach defined in this article is very rudimentary and if it is used as is, then expert user can circumvent this version checking (full/trial) with very little effort. Perhaps using this approach in conjunction with encryption/decryption and little bit customization of logic will make it more robust for commercial use.

Note: This plugin uses a registry component found at CodeProject here.

Using the Code

What we are doing here is we are keeping a track of number of times the application has been run in Windows registry. We will check for the number of times the application has been opened. If the user is allowed to open the application, we will let him run the application and increase the count in the registry. If the number of count increases the acceptable runs in trial version, then we will not start the application.

If the user chooses to make this application a full version, then there is a separate registry entry. A simple code to change the registry value will convert the application to full version without the user needing to install this application again.

Here is the simple class that is handing this logic:

C#
class VersionChecker
{        
    static int currentRun = 1;
    static string RegistryPath = "Software\\Rahul";
    static string TrailVersionKey = "TVS";
    static string FullVersionKey = "FVS";
    const int TRAIL_RUNS = 5;

    public static bool IsValidVersion()
    {
        RegistryComponent registry = new RegistryComponent(RegistryPath, false);

        string strFullVersion = registry.Read(FullVersionKey);
        string strTrailVersion = registry.Read(TrailVersionKey);

        // If this exist then the user is perhaps using the full version
        if (strFullVersion == null)
        {
            //this could be the first run
            if (strTrailVersion == null)
            {
                //this is the first run definitely
                registry.Write(TrailVersionKey, currentRun.ToString());
            }
            else
            {
                //not the first run
                currentRun = Convert.ToInt32(strTrailVersion);
                currentRun += 1;

                registry.Write(TrailVersionKey, currentRun.ToString());

                //Let us check whether is has exhausted all his trail attempts or not
                if (currentRun > TRAIL_RUNS)
                {
                    //The user has exhausted all his trail runs, do not let him in
                    return false;
                }
            }
        }

        //User is either using the full version or still has some trails left, let him pass through
        return true;
    }
}

To use this component, we will have put a check at the application start up as:

C#
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    if (VersionChecker.IsValidVersion() == true)
    {
        Application.Run(new Form1());
    }
    else
    {
        MessageBox.Show("Your trail version has expired, Please get the full version");
    }
}

The application will only run for 5 times in this particular case. 

Points of Interest

This small code facilitates having a trial version for any Windows application. However if the registry editing is not allowed on a user machine, then this approach will not work. Then we will have to keep a separate configuration file keeping track of this information.

There is a lot of scope for improvement in this application. We can have features like encryption/decryption, custom logic instead of counts, having some particular key written while updating to full version to make this more robust.

Also, with some changes, we can easily change this plugin to have a time based trial version. We just have to track time instead of count in that case.

Note: The core of this approach is the class handling registry editing which can be found here.

History

  • 23 April 2012: First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)