これは私のコードです。テキスト ファイルから読み取り、フォームのリスト ボックスに配置します。テキストファイルにすでに何かがある場合は機能します。しかし、テキスト ファイルの内容を削除してプログラムを実行すると、プログラムがクラッシュし、「インデックスが配列の範囲外にありました」というエラーが表示されます。
でassignment.Request.Name = columns[1];
、それを修正する理由/方法がわかりません。
public static List<Assignment> GetAssignment()
{
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
StreamReader textIn =
new StreamReader(
new FileStream(path3, FileMode.OpenOrCreate, FileAccess.Read));
List<Assignment> assignments = new List<Assignment>();
while (textIn.Peek() != -1)
{
string row = textIn.ReadLine();
string[] columns = row.Split('|');
Assignment assignment = new Assignment();
assignment.Employee.Name = columns[0];
assignment.Request.Name = columns[1];
assignments.Add(assignment);
}
textIn.Close();
return assignments;
}