简体   繁体   中英

Trying to use StreamWriter and StreamReader to create a login system but it doesn't work?

Even though the Log.txt file contains the usernames and passwords I am trying to log into, the following STILL isn't working.

This is my REMADE login code:

public void button1_Click(object sender, EventArgs e)
    {
        bool loggedin = false;
        Form2 form2 = new Form2();
        Form1 form1 = new Form1();
        var contents = File.ReadAllLines(@"C:\log.txt");
        if (contents.Contains(Username.Text))
                {
                    loggedin = true;
                    MessageBox.Show("Login sucessfully", "Program", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    form1.Hide();
                    form2.Show();
                }

                else
                {
                    if (loggedin == false)
                    {


                        MessageBox.Show("Invalid username or password.", "Program", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

        }

This is my registration code:

    {
        using (System.IO.StreamWriter writer = new System.IO.StreamWriter("C:\\log.txt", true))
        {
            writer.WriteLine("\r" + "Username : " + textBox1.Text);
            writer.WriteLine("Password : " + textBox2.Text);
            writer.WriteLine("Age : " + textBox4.Text);
        }

        Form5 form5 = new Form5();
        Form1 form1 = new Form1();
        form1.Show();
        this.Hide();
    }

Any ideas?

  1. Your code isn't working because simply seeing if the string "log.txt" contains the username doesn't actually open the file and check the contents.

  2. There's no need to use a StreamReader / StreamWriter to read the text of the file. You just need to open the file, read the contents, and then check against those:

     contents = File.ReadAllLines(@"C:\\Path\\To\\File\\log.txt"); if(contents.Contains(Username.Text)) // ... 
  3. I sincerely hope you don't expect this system to be in the least bit secure. Storing all of your username/password combinations in a plaintext file is terrible. Anybody could open it, read it, and see every single combination in the file. Also, all I have to do is guess some random string in your file. My best bet would be to guess a single letter. I'll have a 1/26 chance that character exists in your file and I'll be granted access.

Your code is not doing what you think it is doing.

if ("log.txt".Contains(Username.Text))

This is simply checking if "log.txt" (as a string, not a file) contains the text contained within the username field.

Assuming that you're trying to check only if a username exists, you could do something like this:

using System.Linq;

// ...

var usernames = File.ReadAllLines(@"C:\log.txt");
if (usernames.Contains("Username : " + Username.Text)) {
    // Username exists

Note that the code above doesn't take any care for a password of any type, and is certainly not production-ready in anyway. You shouldn't be using a text file to store your credentials at all and should consider using a database, with appropriate hashing and whatnot for passwords. I'd consider learning a bit more about this practice before trying to put it into your application as this is fairly basic.

The first indication relating to your question should be that you're not actually using StreamReader or StreamWriter anywhere.

if("log.txt".Contains(Username.Text)

这是在检查实际的字符串“ log.txt”是否包含用户名,并且没有靠近任何文件。

You are using "Log.txt" as a string. You need to search the actual value in the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM