我已经将它创建为一个表单。如果收到所有信息并且似乎可以工作,但是当我检查文本文件时,那里没有写任何东西,它只让我在出错之前运行表单两次。有人看到问题了吗?
const string FileName = "Friends.txt";
Friend friend = new Friend();
FileStream file = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
FileStream file2 = new FileStream(FileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
public Form1()
{
InitializeComponent();
}
private void enter_Click(object sender, EventArgs e)
{
StreamWriter write = new StreamWriter(file2);
try
{
friend.FirstName = firstName.Text;
friend.LastName = lastName.Text;
friend.PhoneNumber = phoneNumber.Text;
friend.Month = Convert.ToInt32(birthMonth.Text);
friend.Day = Convert.ToInt32(birthday.Text);
write.WriteLine(friend.ToString());
MessageBox.Show("Wrote " + friend.ToString() + " to file.");
}
catch(Exception error)
{
MessageBox.Show(error.Message + " Please reenter the information.");
}
firstName.Clear();
lastName.Clear();
phoneNumber.Clear();
birthMonth.Clear();
birthday.Clear();
write.Close();
file2.Close();
}