Display an end user license agreement (EULA) in C#


Many program display an end-user license agreement (EULA) when they are installed or the first time they are run. This example uses a form called EulaForm to display a RichText format EULA file. The form returns DialogResult.OK if the user clicks Accept and DialogResult.Cancel if the user clicks Decline.

The following code shows how the main program displays the form.

private void Form1_Load(object sender, EventArgs e)
    // See if the user has already accepted the EULA.
    if (RegistryTools.GetSetting(
        "howto_eula_once", "EulaIsAccepted", "False").ToString()
            != "True")
        // Display the EULA.
        EulaForm frm = new EulaForm();
        if (frm.ShowDialog() == DialogResult.OK)
            // Remember that the user accepted the EULA.
                "howto_eula_once", "EulaIsAccepted", "True");
            // The user declined. Close the program.

    // Perform other initialization stuff.

The code first uses the GetSetting method described shortly to see whether the EulaIsAccepted value stored in the registry is true. If the value is not true, then the user has not previously accepted the EULA, so the program displays the EulaForm.

If the user accepts the EULA, the code sets EulaIsAccepted to true in the registry so the program does not show the form in the future. If the user declines the EULA, the program exits.

To make using the EULA form easier to use, its Accept button’s DialogResult property is set to OK and its Decline button’s DialogResult is set to Cancel. When the user clicks a button, the button sets the form’s DialogResult property accordingly and that automatically hides the form, returning the appropriate result to the main program’s call to ShowDialog.

The EulaForm uses the following code to load the RichText format file Eula.rtf into its RichTextBox control.

// Load the EULA file.
private void EulaForm_Load(object sender, EventArgs e)

To make it easy for the program to find the EULA file at run time, add it to the project by opening the Project menu and selecting Add Existing Item. Select the file and click Add.

Next select the file in Solution Explorer and use the Properties window set the file’s Copy to Output Directory property to Copy if Newer. Now Visual Studio will copy the latest version of the EULA file into the executable directory so the program can find it.

The final pieces of interesting code in this project are the following GetSetting and SaveSetting methods.

// Save a value.
public static void SaveSetting(string app_name, string name,
    object value)
    RegistryKey reg_key =
        Registry.CurrentUser.OpenSubKey("Software", true);
    RegistryKey sub_key = reg_key.CreateSubKey(app_name);
    sub_key.SetValue(name, value);

// Get a value.
public static object GetSetting(string app_name, string name,
    object default_value)
    RegistryKey reg_key =
        Registry.CurrentUser.OpenSubKey("Software", true);
    RegistryKey sub_key = reg_key.CreateSubKey(app_name);
    return sub_key.GetValue(name, default_value);

These methods open the registry’s CURRENT_USER hive, go into the Software key, and create a subkey named after the application. They then get or set a value inside the application’s key.

The registry stores separate values for each user’s CURRENT_USER hive, so each user must accept the EULA when first running the program.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in forms, programs, registry and tagged , , , , , , , , , . Bookmark the permalink.

4 Responses to Display an end user license agreement (EULA) in C#

  1. Richard Arnold says:

    Your code references “RegistryTools”, i.e., if (RegistryTools.GetSetting(…
    Where are you defining RegistryTools? Is this a helper utility?
    Your code calls it, but it isn’t clear where or how its defined.

    Please provide some explanation regarding it. Thank you.

    • RodStephens says:

      Sorry, that is a bit unclear. RegistryTools is the helper class that contains the SaveSetting and GetSetting methods described in the post. Those are the last pieces of code.

      Often the posts don’t include every last detail about a project. If you download the example, you can see all of the details. In this example, you could right-click on “RegistryTools” in the code editor and select “Go To Definition” (or something similar in different versions of Visual Studio) to see the class’s definition. Then if this was defined somewhere else, such as in a .NET Framework library, the code editor will show you where.

    • Richard Arnold says:

      Never mind. I figured out that “RegistryTools” was a class file.
      After creating the class, I was able to both implement the code and successfully test it.

      Thank you very much for your EULA example.

  2. Marco says:

    Nice stuff. Thanks. I used the Properties.Settings.Default instead.
    Kind regards 🙂

Comments are closed.