Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将密钥存储在同一目录中,并在应用程序启动时对其进行验证_C# - Fatal编程技术网

C# 将密钥存储在同一目录中,并在应用程序启动时对其进行验证

C# 将密钥存储在同一目录中,并在应用程序启动时对其进行验证,c#,C#,我有两张Windows应用程序表单。在第一种形式中,我有一个按钮,在第二种形式中,我有一个按钮和一个文本框 当我点击第一个表单中的按钮时,程序应该检查产品是否已经激活。如果已激活,则应继续使用form1,否则应调用form2请求输入产品密钥 一旦产品密钥输入到表单2中中,程序应验证它并将其存储在同一目录中的文本文件中 当我下次运行该程序时,在单击form1中的按钮后,它不应该要求再次激活它 我有下面的代码,但我没有得到这个完整的功能。请帮我解决这个问题,也请在阅读或编写文本文件时提供例外帮助 /

我有两张Windows应用程序表单。在第一种形式中,我有一个按钮,在第二种形式中,我有一个按钮和一个文本框

当我点击第一个表单中的按钮时,程序应该检查产品是否已经激活。如果已激活,则应继续使用
form1
,否则应调用
form2
请求输入产品密钥

一旦产品密钥输入到
表单2中
中,程序应验证它并将其存储在同一目录中的文本文件中

当我下次运行该程序时,在单击
form1
中的按钮后,它不应该要求再次激活它

我有下面的代码,但我没有得到这个完整的功能。请帮我解决这个问题,也请在阅读或编写文本文件时提供例外帮助

// FIRST FORM

private void fixButton_Click(object sender, EventArgs e)
{
    TextReader tr = new StreamReader("Product-Key.txt");

    if ( (tr.ToString() == "TGR2W2") || (tr.ToString() == "8DTT9M") )
    {
        MessageBox.Show ( "product is already activated ");
    }
    else 
    {
        Form2 secondForm = new Form2();
        secondForm.Show();
    }
}
//第二种形式的激活按钮

private void activateButton_Click(object sender, EventArgs e)
{
    if ((productkeyTextBox.Text == "TGR2W2") || (productkeyTextBox.Text == "8DTT9M") )
    {
        // to store the inputted product key in a tex file of the same directory
        keyStore ((productkeyTextbOX.Text).ToString());
        this.Close();

        MessageBox.Show(" The product has been succesfully activated");
        // NEED HELP HERE IN LOADING THE FORM 1 BACK AFTER STORING THE PRODUCT KEY 
     }
     else
     {
         MessageBox.Show("Please enter a valid 16 digit product key!!");
     }
}

public void keyStore(string key)
{
    try
    {
        // create a writer and open the file
        TextWriter tw = new StreamWriter("Product-Key.txt");

        // write a line of text to the file
        tw.WriteLine(key);

        // close the stream
        tw.Close();
    }
    catch (IOException)
    {
    }
}

你所说的“没有获得完整的功能”是什么意思?你所得到的例外是什么?顺便说一句,你可以简化你正在做的很多文件I/O。。。您可能可以使用File.ReadAllText(“Product Key.txt”)从文件中读取密钥,而使用File.WriteAllText(“Product Key.txt”,productkeyTextbOX.Text)将密钥写入文件。还有一件事。。。在fixButton\u-Click方法中,您试图为一个可能还不存在的文件打开一个流,并且您没有将其包装在try/catch中。如果用户单击按钮并且文件还不存在,则您的代码(如图所示)将引发未处理的异常。