0

I want to create a simple one time activation process for my windows form application. So, I basically have two forms, form1 is the activation window and form2 is the actual program. I've create a very basic activation program in form1 given below

string mac = textBox1.Text;
            string str1 = mac.Substring(4,1);
            string str2 = mac.Substring(5,1);
            string str3 = mac.Substring(7,1);
            string str4 = mac.Substring(2, 1);
            string pattern = str1 + str2 + str2 + str3 + str4;
            if (textBox2.Text == pattern)
            {
                MessageBox.Show("Program activated!!!");
                Form2 n = new Form2();
                n.Show();
                this.Hide();
            }
            else { MessageBox.Show("Wrong key"); }

Now, the problem is every time I load my program it always loads form1 even when someone successfully entered the key(i.e. pattern) once before. How do I store that information so that if someone enters the correct key, every time after that whenever the program is loaded it will automatically show form2 (i.e. my actual program) and skip form1. BTW, I'm aware that there are other more advanced and secure ways of doing this but I'm just currently interested in this very basic method. Can anyone help?

Aryan Firouzian
  • 1,940
  • 5
  • 27
  • 41
Bumba
  • 321
  • 3
  • 13
  • 1
    Consider using the registry or a file. – SLaks Nov 01 '17 at 17:09
  • You should name your forms and controls. – SLaks Nov 01 '17 at 17:09
  • You could decide in Program.cs which form to load as the main form. – Psi Nov 01 '17 at 17:10
  • @SLaks How do I use the registry or a file? – Bumba Nov 01 '17 at 17:10
  • Have a look at the class `Microsoft.Win32.Registry` or the namespace `System.IO`. There's tons of tutorials out there, just search for them using the search engine of your choice (that means: Google) – Psi Nov 01 '17 at 17:20
  • Check out "Settings" in your IDE for the project. You have the option of storing custom User or Machine level settings for an application. You could compare a setting value at execution time, prior to loading any forms. –  Nov 01 '17 at 17:56
  • You could just have it create a text file the first time the user enters the key, and have it check for the file. There's a lot of things that could go wrong with doing something like that, but its probably the most basic way to do what you want to do. – jdwee Nov 01 '17 at 18:06

1 Answers1

0

Here's one very rudimentary way - write a file to a known location when they activate, then check for the existence of that file each time you load the form. If it's there, immediately show Form2. If not, give them the chance to activate.

Differing methods would be to save the activation status in the Registry or in a Database, or somewhere else, but the overall process is about the same

Sample code:

First, a method to get the path to the file we're going to create:

private string GetActivatedFilePath()
{
    var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    var thisExeName = Path.GetFileNameWithoutExtension(
        System.Reflection.Assembly.GetEntryAssembly().Location);

    return Path.Combine(appDataPath, thisExeName, "Activated.txt");
}

Then a couple of methods to create the file (Activate()), check if the file exists (IsActivated), and delete the file (Deactivate()):

private void Activate()
{
    if (!IsActivated())
    {
        var filePath = GetActivatedFilePath();
        Directory.CreateDirectory(Path.GetDirectoryName(filePath));
        File.Create(filePath);
    }
}

private bool IsActivated()
{            
    return File.Exists(this.GetActivatedFilePath());
}

private void Deactivate()
{
    if (IsActivated())
    {
        File.Delete(GetActivatedFilePath());
    }
}

Then we can also create a method to show the second form, since we will be calling this in more than one place. I've modified this to first hide the current form, then show the second form as a dialog (which means they can't switch back to the main form and code will pause in the first form until the second one closes), and then close the first form when the second one closes:

private void ShowForm2()
{
    Form2 n = new Form2();
    this.Hide();
    n.ShowDialog();
    this.Close();
}

Now we can check if they're activated in our Form_Load event, and if they are then immediately show the second form:

private void Form1_Load(object sender, EventArgs e)
{
    // If they user is already activated, show the second form immediately
    if (IsActivated())
    {
        ShowForm2();
    }
}

And then your current code can also make use of these functions to activate the user. I'm assuming the code lives behind an Activate button:

private void btnActivate_Click(object sender, EventArgs e)
{
    bool activated = false;

    if (textBox1.Text.Length > 7)
    {
        string mac = textBox1.Text;
        string str1 = mac.Substring(4, 1);
        string str2 = mac.Substring(5, 1);
        string str3 = mac.Substring(7, 1);
        string str4 = mac.Substring(2, 1);
        string pattern = str1 + str2 + str2 + str3 + str4;

        activated = textBox2.Text == pattern;
    }

    if (activated)
    {
        MessageBox.Show("Program activated!!!");
        Activate();
        ShowForm2();
    }
    else
    {
        MessageBox.Show("Wrong key");
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43