用户名和密码错误C#

时间:2017-03-15 13:08:17

标签: c#

我是C#的新手,并尝试创建用户登录窗口。  目前我正在尝试从txt文件中搜索,如果输入正确的用户名,它可以正常工作。但问题是当输入错误的用户名时 - 它会陷入循环。

实际上我从文本框中读取字符串的方式是错误的,如果我输入错误的用户名,它不允许我再次输入新字符串。它不断比较旧值或空值 任何人都可以指导如何做到这一点吗?

public void userpass()
{
    int us = 0; //for user pass
    string readText2 = File.ReadAllText(pathuser);
    using (StreamReader sr2 = new StreamReader(pathuser))
    {
        string usernam = username.Text;
        string line;
        string[] lines = new String[500];

        while ((line = sr2.ReadLine()) != null)
        {
            lines[us] = line;


            if (lines[us] == usernam)
            {
                check = 1;
                MessageBox.Show(usernam);
                Form2 f2 = new Form2();
                this.Hide();
                break;
            }
            us++;
        }

        if (lines[us] != usernam && usernam != null)
        {
            this.DialogResult = DialogResult.None;
            DialogResult result = new DialogResult();
            result = MessageBox.Show("Invalid Username or Password?", 
                                     "Retry",
                                     MessageBoxButtons.OK);
            if (result == DialogResult.OK)
            {
                username.Clear();

            }
        }
    }

3 个答案:

答案 0 :(得分:0)

while ((line = sr2.ReadLine()) != null)
{
    lines[us] = line;

    if (lines[us] == usernam)
    {
        check = 1;
        MessageBox.Show(usernam);
        Form2 f2 = new Form2();
        this.Hide();
        break;
    }
    us++;
}

问题在于你的while循环。在文件的第一次迭代中设想line = "incorrectUsername"。您正在分配"不正确的用户名"行[0]然后行[1]然后行[2]等...它永远不会停止。您只需将无效的用户名分配给数组,然后将输入的用户名与该值进行比较。你想要做的就是放弃"线"数组,只需将用户名与行进行比较:

if (line.Equals(username))
{
   //Go to new form
}

答案 1 :(得分:0)

在这个简单的例子中,您不需要使用StreamReader,这是一种更简单的方法:

string myUserName = "Admin";

    string[] lines = File.ReadAllLines(usersTextFile, Encoding.UTF8);

    bool found = false;

    if (lines != null)
    {
        foreach (var l in lines)
        {
            if (string.IsNullOrEmpty(l))
                continue;

            if (l.ToLower() == myUserName.ToLower())
            {
                found = true;
                break;
            }
        }
    }

    if (found)
        MessageBox.Show("Welcome " + myUserName + "!", "Protected", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    else
        MessageBox.Show("User not found!", "Protected", MessageBoxButtons.OK, MessageBoxIcon.Error);

答案 2 :(得分:0)

我的完整代码是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Employees_1
{
    public partial class Form2 : Form
    {

        string pathuser = @"//192.168.1.10/Shared-Public/testfile.txt";
        int check = 0;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //  string username = username.Text;

        }

        private void button1_Click(object sender, EventArgs e)
        {

            //string usernam = username.Text;
            // MessageBox.Show(usernam);


            while (check != 1)
            {
                userpass();


            }
            this.Close();


        }

        public void userpass()
        {
            int us = 0; //for user pass
            string readText2 = File.ReadAllText(pathuser);
            using (StreamReader sr2 = new StreamReader(pathuser))
            {
                string usernam = username.Text;
                string line;
                string[] lines = new String[500];

                while ((line = sr2.ReadLine()) != null)
                {
                    lines[us] = line;


                    if (lines[us] == usernam)
                    {
                        check = 1;
                        MessageBox.Show(usernam);
                        Form2 f2 = new Form2();
                        this.Hide();
                        break;
                    }
                    us++;
                }

                if (lines[us] != usernam && usernam != null)
                {
                    this.DialogResult = DialogResult.None;
                    DialogResult result = new DialogResult();
                    result = MessageBox.Show("Invalid Username or Password?", "Retry", MessageBoxButtons.OK);
                    if (result == DialogResult.OK)
                    {

                        //DialogResult result = MessageBox.Show("Invalid Username or Password", "Error", MessageBoxButtons.OK);
                        // MessageBox.Show("Invalid Username or Password");
                        username.Clear();


                    }
                }
            }


         /*   string[] lines = File.ReadAllLines(pathuser, Encoding.UTF8);

            bool found = false;
            string usernam = username.Text;
            if (lines != null)
            {
                foreach (var l in lines)
                {
                    if (string.IsNullOrEmpty(l))
                        continue;

                    if (l.ToLower() == usernam.ToLower())
                    {
                        found = true;
                        break;
                    }
                }
            }

            if (found)
                MessageBox.Show("Welcome " + usernam + "!", "Protected", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            else
                MessageBox.Show("User not found!", "Protected", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }
            */

    }
}