如何通过文件处理修复c#中的用户名验证?

如何通过文件处理修复c#中的用户名验证?,c#,file-handling,unhandled-exception,C#,File Handling,Unhandled Exception,这段代码是我程序的一小部分。它基本上读取存储全名、银行余额、用户名和密码的用户信息文件 我的问题是,为什么当我输入一个不在文件中的用户名时,如果有一个if语句说如果找不到用户名,那么就转到register方法,它就会抛出一个错误 public static int player; public static void Username_Check() { string[] str = File.ReadAllText(@"X:\btec computing

这段代码是我程序的一小部分。它基本上读取存储全名、银行余额、用户名和密码的用户信息文件

我的问题是,为什么当我输入一个不在文件中的用户名时,如果有一个if语句说如果找不到用户名,那么就转到register方法,它就会抛出一个错误

    public static int player;
    public static void Username_Check()
    {
        string[] str = File.ReadAllText(@"X:\btec computing\unit 1\C sharp\online_casino_prog\user_info.csv").Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        string[] users = new string[str.Length];
        Console.WriteLine("Enter your username. ");
        string username = Console.ReadLine();
        bool user_found = false;
        for (int i = 0; i < (str.Length); i++)
        {
            string[] person = str[i].Split(',');
            if (person[2] == username)
            {
                Console.WriteLine("Welcome back {0}!", person[0]);
                user_found = true;
                player = i;
                Password_Check();
            }
        }
        if (user_found == false)
        {
            Console.WriteLine("Sorry, we could not find an account linked to the username '{0}', Please register an account with us! ",username);
            Register();
        }
    }
公共静态int播放器;
公共静态无效用户名\u检查()
{
string[]str=File.ReadAllText(@“X:\btec computing\unit 1\C sharp\online\u casino\u prog\user\u info.csv”).Split(新字符串[]{Environment.NewLine},StringSplitOptions.None);
字符串[]用户=新字符串[str.Length];
WriteLine(“输入您的用户名”);
字符串username=Console.ReadLine();
bool user_found=false;
对于(int i=0;i<(str.Length);i++)
{
字符串[]person=str[i].Split(',');
如果(个人[2]==用户名)
{
WriteLine(“欢迎回来{0}!”,person[0]);
用户_found=true;
player=i;
密码检查();
}
}
if(user_found==false)
{
WriteLine(“对不起,我们找不到链接到用户名“{0}”的帐户,请向我们注册一个帐户!”,用户名);
寄存器();
}
}
这就是我得到的错误:

未处理的异常:System.IndexOutOfRangeException:索引超出数组的边界。 在X:\btec computing\unit 1\C sharp\online\u casino\u prog\online\u casino\u prog\Program.cs:第58行的online\u casino\u prog.Program.Username\u Check()中 在X:\btec computing\unit 1\C sharp\online\u casino\u prog\online\u casino\u prog\Program.Main(字符串[]args)中的online\u casino\u prog.cs:第30行


可以使用LINQ简化代码

var username = Console.ReadLine();
var lines = File.ReadAllLines("./pathtoyour.csv").Select(line => line.Split(',')); // reads all lines from your csv then each line is transformed into an array of strings
var user = lines.FirstOrDefault(line => line[2] == username); // gets the first occurrence of the username, if no user is found returns null, otherwise user variable will be an array with the row data 

if (user != null)
  Console.WriteLine("Call PasswordCheck()");
else
  Console.WriteLine("Call Register()");

请包括您得到的错误。您的编译器/调试器已经完成了查找错误的工作,请不要因为不告诉我们您的编译器/调试器找到了什么而让我们再次执行相同的工作。您的文本文件末尾是否有一个空行?甚至是最后一行加上新行?(或中间的任何空行)如果是这样的话,
str[str.length-1]==”
,如果将其拆分,则会在
person[2]
上出现错误。如果(person.length>2)您的错误与您发布的代码不匹配,请添加一项检查。这可能不在你发布的代码中。请提供一份报告。现在可能是学习如何使用调试器的好时机。找一本自己选择的书或教程,找出出现此错误的原因。这正是程序崩溃的原因,因为您没有在Console中输入有效的数字。ReadLine()关于:调试,这很公平。更新后的错误证实了我前面所说的-您的输入没有3个逗号分隔的列,可能是因为它是空的。你加上我建议的支票了吗?那很酷。这会消除我在那里的所有代码吗?尝试在项目中实现它,您仍然需要设置
player
变量