【问题标题】:How do I read one line for a .txt file in c#?如何在 c# 中读取 .txt 文件的一行?
【发布时间】:2019-03-02 04:52:07
【问题描述】:

我正在尝试创建一个具有登录功能和注册功能的密码数据库。通过将姓名、姓氏、电子邮件和密码保存到 .txt 文件中,我可以使用注册功能,但是,我无法让程序读取一行,然后检查用户输入是否与密码中的密码匹配.txt 文件。 这是我目前正在使用的内容。

//reading from file

int counter = 0;

private void Enter_btn_Click(object sender, EventArgs e)
{
    // makes a new file called password.txt
    StreamReader sr = new StreamReader("password.txt");

    string Line = "";
    //this reads all lines in the .txt file 
    while ((Line = sr.ReadLine())!=null)
    {
        //loops through each line.
        counter++;
        break;
    }
}

我希望 while 循环只查看一行,然后对于用户名,我想检查另一行并验证用户名是否正确。

counter++;

break;

我希望它通过第一行然后中断和第一行。我正在使用表单应用程序在 Visual Studio 中工作。

【问题讨论】:

  • 从不将密码存储为纯文本。使用例如而是散列值。
  • @dymanoid 是正确的,但这不是真正的问题 OP 希望帮助逐行阅读内容
  • @Ethan - 如果你的文本文件看起来像这样,你能给我们举个例子吗? 1 行
  • 使用File.ReadLines() 一次读取一行。您可以使用string line = File.ReadLines(filename).First() 来获取第一行。
  • @Dave,这就是我将其发布为评论而非答案的原因。也许,OP 会重新考虑他们的解决方案,所以这个问题将变得无关紧要。

标签: c# passwords


【解决方案1】:

您可以使用 if 语句而不使用循环,但您必须手动执行。

例如:

//reading from file

int counter = 0;

private void Enter_btn_Click(object sender, EventArgs e)
{
    // makes a new file called password.txt
    StreamReader sr = new StreamReader("password.txt");

    string usr = "";
    string pass = "";

    pass = sr.ReadLine();
    if(pass != null)
    {
        //You get the password here now you can do the logic
    }
    else
    {
        //There is no line should throw an exception for instance
    }

    //Now lets get the username
    usr = sr.ReadLine();
    if(usr != null)
    {
        //You get the usr here
    }
    else
    {
        //There is no 2nd line should throw an exception for instance
    }

}

【讨论】:

  • 所以 If (pass!=null) 将从 .txt 行和括号中的行中获取密码,我可以检查它是否匹配?
  • 其实就是“pass = sr.ReadLine();”将获取密码,if 条件将检查文本文件中是否有该行,如果有行则您可以检查内部是否与用户输入匹配。
  • 我不太确定如果你再次调用 sr.ReadLine() 如果前一个返回 null 会发生什么。我认为它会引发异常,但我不确定。这就是为什么我建议你在 else 语句中抛出异常。
【解决方案2】:

您可以读取第一行而不是逐行迭代。

int counter = 0;

private void Enter_btn_Click(object sender, EventArgs e)
{
    // makes a new file called password.txt
    StreamReader sr = new StreamReader("password.txt");

    string Line = sr.ReadLine();
    //this reads all chars in the line 
    foreach (var ch in Line)
    {
        //loops through each char.
        counter++;
        break;
    }
}

如果你只想要长度,那么你可以使用Line.length。

【讨论】:

    【解决方案3】:

    你可以这样做:

    StreamReader sr = new StreamReader("password.txt");
    string user;
    while((user = sr.ReadLine()) != null)  
    {  
        string password;
        if ((password = sr.ReadLine()) == null) {
            // TODO: Throw some exception for example Illegal State.
        }
        if (string.Equals(user, #user)) {
            if (string.Equals(password , #password)) {
                sr.Close();
                // SUCCESS
            } else {
                // WRONG PASSWORD
            }
        }
    }
    sr.Close();
    // WRONG USER
    

    【讨论】:

    • 我的 txt 文件是怎样的,密码在文档的第 9 行,然后用户名在第 7 行,然后我希望它从这些行中读取,我可以验证用户和密码
    • 正常的方法是使用另一种格式,例如。 xml,那里有很多库。如果没有,只需一次读取块的所有行,解析并检查它们
    【解决方案4】:

    您可能想要更改插入数据的方式。

    尝试在值之间插入制表符 (\t) 并以 (\n) 结束记录,以便您可以读取整个文件并使用 String.Split('\n') 这将为您提供单独的记录并再次拆分通过 String.Split('\t') 获取用户名和密码组合的单个记录..

    然后你可以有逻辑来验证凭据

    【讨论】:

    猜你喜欢
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    相关资源
    最近更新 更多