【发布时间】:2011-08-13 06:31:11
【问题描述】:
好的,我正在尝试通过 C# 加载一堆配置文件,当我尝试启动程序时不断收到此错误。
C:\C#FILES>program.exe
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the boun
ds of the array.
at ConsoleApplication2.Program.loadAccounts()
at ConsoleApplication2.Program.Main(String[] args)
C:\C#FILES>
我已经调查过,我认为这与文件中帐户的格式有关 我想知道正确的方法是什么,我已经尝试了所有我能想到的方法
这是加载帐户的方法
private static void loadAccounts()
{
using (TextReader tr = new StreamReader("accounts.txt"))
{
string line = null;
while ((line = tr.ReadLine()) != null)
{
String[] details = line.Split('\t');
accounts.Add(details[0] + ":" + details[1]);
}
}
}
accounts.txt 部分是我不确定的部分,我认为应该如下 用户名(标签)密码 像这样
username password
但是它给出了上面显示的错误 有谁知道正确的帐户格式应该是什么?
【问题讨论】:
-
我遗漏了一些东西...您是否在编辑器中检查过文件? details[0] 或 details[1] 都不存在。数据在读取文件时是否以格式实际存在?一边走一边打印线怎么样,这样你就知道是哪一条了?
标签: c# arrays indexing user-accounts