WindowsForms(c#)上的登录应用程序和Streamreader

WindowsForms(c#)上的登录应用程序和Streamreader,c#,C#,尽管我一直在使用这个网站来学习和解决一些问题,但这是我第一次问问题 我正在开发一个简单的登录应用程序,它应该读取两个文本文件(一个带有用户名,另一个带有密码),然后将它们与各自文本框中的文本进行比较 string user, pass; string pathtouser = @"C:\Users.txt";/*Both are paths */ string pathtopass = @"

尽管我一直在使用这个网站来学习和解决一些问题,但这是我第一次问问题

我正在开发一个简单的登录应用程序,它应该读取两个文本文件(一个带有用户名,另一个带有密码),然后将它们与各自文本框中的文本进行比较

    string user, pass;
    string pathtouser = @"C:\Users.txt";/*Both are
                                         paths */
    string pathtopass = @"C:\Pass.txt";

    private void button1_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
    Login logeo = new Login();
    private void button2_Click(object sender, EventArgs e)
    {
        logeo.openFile();
    }
    private void loginbutt_Click(object sender, EventArgs e)
    {
        StreamReader Read = new StreamReader(pathtouser);
        StreamReader Reader = new StreamReader(pathtopass);
        user = Read.ReadToEnd();
        pass = Reader.ReadToEnd();
        //Here we read the textfiles and add the string to the variables (user and pass)

        if (Usertext.Text == user && passtext.Text == pass)
        {
            testing test = new testing();
            test.Show();
        }//New window it should open if username and password inserted in the textboxes are correct.
        else
        {
            MessageBox.Show("User or password is incorrect. Please verify!!", "WARNING!!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }//Denies access and shows a warning.
它工作得很好。。。仅验证第一行。 问题是,我在未读取的文本文件上又分配了大约两个用户和密码,因此我无法使用这些用户和密码登录

我的一个朋友建议我应该利用ASCII码来查找包含所需用户名和密码的整个字符串,并使用for循环


作为初学者,有没有更好的方法?(我还得学习阵列)

你可以尝试以下方法

List<string> users = new List<string>(File.ReadAllLines(pathtouser));
List<string> passwords = new List<string>(File.ReadAllLines(pathtopass));
int index = users.IndexOf(Usertext.Text);
if (index != -1 && index < passwords.Count)
{
    if (passtext.Text == passwords[index])
    {
        testing test = new testing();
        test.Show();
        return;
    }
}
else
{
    MessageBox.Show("User or password is incorrect. Please verify!!", "WARNING!!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
List users=新列表(File.ReadAllLines(pathtouser));
列表密码=新列表(File.ReadAllLines(pathtopass));
int index=users.IndexOf(Usertext.Text);
如果(索引!=-1&&index
您还可以读取Users.txt中的每一行,并比较Pass.txt文件中相应位置保存的密码。因此,作为初学者,您可以首先尝试理解以下代码,然后继续改进它们:

int lineCounter = 0;
string user;
List<string> passwords = new List<string>(File.ReadAllLines(pathtopass));//store passwords in a List array

// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader("c:\\Users.txt");
while((user = file.ReadLine()) != null)//this will read each line at a time till it reaches the end of the file
{
   if(Usertext.Text == user)//check for the user
   {
   try{
        if (passtext.Text == passwords[lineCounter])//check for the password stored in the respective line in the Password.txt
        {
            //do your thing-->
            testing test = new testing();
            test.Show();
            return;
        }
        else
        {
            MessageBox.Show("User or password is incorrect. Please verify!!", "WARNING!!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }//Denies access and shows a warning.
    }
    catch(Exception frog)//catch any exception which might arise here
    {
        MessageBox.Show("Error: "+frog.Message.ToString());
    }
   }
   lineCounter++;//increment the counter to fetch the next index
}

file.Close();//close the file after reading is complete
int lineCounter=0;
字符串用户;
列表密码=新列表(File.ReadAllLines(pathtopass))//将密码存储在列表数组中
//读取文件并逐行显示。
System.IO.StreamReader file=new System.IO.StreamReader(“c:\\Users.txt”);
while((user=file.ReadLine())!=null)//这将一次读取每一行,直到它到达文件的末尾
{
if(Usertext.Text==user)//检查用户
{
试一试{
if(passtext.Text==passwords[lineCounter])//检查password.txt中相应行中存储的密码
{
//做你的事-->
测试=新测试();
test.Show();
返回;
}
其他的
{
MessageBox.Show(“用户或密码不正确。请验证!!”、“警告!!”、MessageBoxButtons.OK、MessageBoxIcon.Hand);
}//拒绝访问并显示警告。
}
catch(Exception frog)//捕获此处可能出现的任何异常
{
Show(“错误:+frog.Message.ToString());
}
}
lineCounter++;//递增计数器以获取下一个索引
}
file.Close()//读取完成后关闭文件

本例使用StreamReader的ReadLine(读取每一行),同时还使用列表数组读取所有行并将其输出。

非常感谢!为了达到我的目的,我不得不玩弄它,但我最终可以用不同的用户和密码测试它,效果非常好!)