【问题标题】:How to create a simple one time activation process using c#?如何使用 c# 创建一个简单的一次性激活过程?
【发布时间】:2018-04-14 01:56:14
【问题描述】:

我想为我的 Windows 窗体应用程序创建一个简单的一次性激活过程。所以,我基本上有两种形式,form1是激活窗口,form2是实际程序。我在下面给出的 form1 中创建了一个非常基本的激活程序

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"); }

现在,问题是每次我加载我的程序时,它总是加载 form1,即使之前有人成功输入了密钥(即pattern)。我如何存储该信息,以便如果有人输入正确的密钥,之后每次加载程序时它都会自动显示 form2 (即我的实际程序)并跳过 form1强>。 顺便说一句,我知道还有其他更先进和更安全的方法可以做到这一点,但我目前只是对这种非常基本的方法感兴趣。 有人可以帮忙吗?

【问题讨论】:

  • 考虑使用注册表或文件。
  • 你应该为你的表单和控件命名。
  • 您可以在 Program.cs 中决定加载哪个表单作为主表单。
  • @SLaks 如何使用注册表或文件?
  • 查看Microsoft.Win32.Registry 类或命名空间System.IO。那里有大量教程,只需使用您选择的搜索引擎(即:谷歌)搜索它们

标签: c# winforms store activation-function


【解决方案1】:

这是一种非常基本的方法 - 在它们激活时将文件写入已知位置,然后在每次加载表单时检查该文件是否存在。如果它在那里,立即显示 Form2。如果没有,让他们有机会激活。

不同的方法是将激活状态保存在注册表或数据库或其他地方,但整体过程大致相同

示例代码:

首先,获取我们要创建的文件的路径的方法:

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");
}

然后有几个方法来创建文件(Activate()),检查文件是否存在(IsActivated),删除文件(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());
    }
}

然后我们还可以创建一个方法来显示第二种形式,因为我们将在多个地方调用它。我已对此进行了修改,首先隐藏当前表单,然后将第二个表单显示为对话框(这意味着他们无法切换回主表单,代码将在第一个表单中暂停,直到第二个表单关闭),然后当第二个表单关闭时关闭第一个表单:

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

现在我们可以检查它们是否在我们的Form_Load 事件中被激活,如果它们被立即显示第二个表单:

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

然后您当前的代码也可以利用这些功能来激活用户。我假设代码位于Activate 按钮后面:

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");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 2011-12-21
    • 2020-10-12
    • 2014-05-14
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多