簡體   English   中英

讀取和寫入文件以進行身份​​驗證

[英]Reading and writing to a file for authentication

我一直在嘗試用 c# 制作一個簡單的身份驗證系統。 我試圖讓它讀取一個文本文件,如果它有正確的答案/用戶名,請繼續。 如果它沒有它有一個文本框。顯示它。 我目前的代碼是:

if(textBox1.Text == "wlkey1")
{
    Hide();
    string fileName1 ="authentication.txt";
    System.IO.File.WriteAllText(fileName1, textBox1.Text);
}
else{
    MessageBox.Show("Yikes. That's incorrect.", "Uh oh.");
}

您正在寫入文件而不是讀取文件。 你必須使用ReadAllText()

string fileName1 ="authentication.txt";
string curPass = "someDefaultPass";
if(System.IO.File.Exists(fileName1)
    curPass = System.IO.File.ReadAllText(fileName1);
if(textBox1.Text == curPass)
{
    Hide();
}
else{
    MessageBox.Show("Yikes. That's incorrect.", "Uh oh.");
}

順便說一下,您可能已經知道,將密碼保存在文件中是不安全的。

為了寫得好,你已經做到了:

string fileName1 ="authentication.txt";
System.IO.File.WriteAllText(fileName1, textBox1.Text);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM