Hi, its me again, I promise this should be quick and simple because I have an idea of what needs to be done.
I'm working on my form (if you saw my other post its basicly a grade book with 5 forms). Is there any simple way to write the student name with the scores to a text file.
From reading my book it looks simple, but I am unsure how to redo my array since it will only be pulling data from a text file and then any new data will be written into the text file.
Here is the starting code I have gotten so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace maint_student_scores
{
public class StudentDB
{
private const string dir = @"C:\C#.NET\Files\";
private const string path = dir + "StudentScores.txt";
public static List<Student> GetStudentScores()
{
// if the directory doesn't exist, create it
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
// create the object for the input stream for a text file
StreamReader textIn =
new StreamReader(
new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));
// create the list
List<Student> students = new List<Student>();
// read the data from the file and store it in the list
while (textIn.Peek() != -1)
{
string row = textIn.ReadLine();
string[] columns = row.Split('|');
Student studentlist = new Student();
studentlist.Name = columns[0];
studentlist.Score = columns[1];
students.Add(studentlist);
}
// close the input stream for the text file
textIn.Close();
return students;
}
public static void SaveStudents(List<Student> studentlist)
{
// create the output stream for a text file that exists
StreamWriter textOut =
new StreamWriter(
new FileStream(path, FileMode.Create, FileAccess.Write));
// write each product
foreach (Student s in studentlist)
{
textOut.Write(s.Name + "|");
textOut.WriteLine(s.Score);
}
// close the output stream for the text file
textOut.Close();
}
}
}
I'm going to guess my above code is correct for the most part, because it looks like it would work.
Since I'm working with multi-forms, would I then set all the List I have in my different forms to null?
I.E.: public List<Student> studentList = null;
that is taken from the first form that is loaded the one that will list all the student names.
I think I'm going in the right direction with this?