为什么我的班级不写入文本文件?

时间:2014-06-01 20:43:50

标签: c# .net

这是我的AddReservation表单代码。请注意,我调用Piper.WritePiper()方法并传入用户输入的Name和Seat。我不确定为什么这不起作用。我没有收到任何错误或任何错误。我只是希望用户能够输入他们想要在飞机上拍摄的名字和座位,然后更新文件。请告诉我我做错了什么...提前谢谢!!

public partial class frmAddReservation : Form
{
    public frmAddReservation()
    {
        InitializeComponent();
    }

    List<Seating> piperSeating = new List<Seating>();
    List<Seating> cessnaSeating = new List<Seating>();

    private void frmAddReservation_Load(object sender, EventArgs e)
    {
        piperSeating = Piper.GetPiperReservations();
        cessnaSeating = Cessna.GetCessnaReservations();
    }


    private void btnShowPiper_Click(object sender, EventArgs e)
    {
        listFlight.Items.Add("Piper Seating Chart:");
        listFlight.Items.Add("");
        //loop through all the seats
        foreach (Seating plane in piperSeating)
        {

            // add the name of plane to the listbox
            listFlight.Items.Add(plane.Name + " " + plane.Seat);

        }
    }

    private void btnAddPiper_Click(object sender, EventArgs e)
    {
        string Name;
        int Seat;

        if (DataValid())
        {
           Name = txtName.Text;
           Seat = Convert.ToInt16(txtSeat.Text);
           Piper.WritePiper(Name, Seat);
        }
    }

    private bool DataValid()
    {
        bool isOK = false;

        if (CheckSeat(txtSeat))
        {
            isOK = true;
        }

        return isOK;
    }

    private bool CheckSeat(TextBox tbox)
    {
        bool isOK = true;

        try
        {
            Convert.ToDouble(tbox.Text);
        }
        catch
        {
            isOK = false;
        }
        return isOK;
    }
}

这是我的Piper.cs类:

public static class Piper
{
    private const string dir = @"Z:\Desktop\Windows 7 Files\C#.net\Reservations\Reservations\Reservations\";
    private const string path = dir + "PiperDat.txt";

    public static List<Seating> GetPiperReservations()
    {
        if (!Directory.Exists(dir))
            Directory.CreateDirectory(dir);

        StreamReader textIn =
            new StreamReader(
                new FileStream(path, FileMode.Open, FileAccess.Read));

        List<Seating> personList = new List<Seating>(); 
        while (textIn.Peek() != -1)
        {
            string row = textIn.ReadLine();
            string[] columns = row.Split(',');
            Seating person = new Seating();
            person.Name = columns[0];
            person.Seat = Convert.ToInt16(columns[1]);
            personList.Add(person);
        }

        textIn.Close();

        return personList;
    }

    public static void WritePiper(string Name, int Seat)
    {
        List<Seating> piperSeating = new List<Seating>();
        piperSeating = Piper.GetPiperReservations();


        StreamWriter textOut =
            new StreamWriter(
                new FileStream(path, FileMode.Open, FileAccess.Write));
        foreach (Seating plane in piperSeating)
        {
            Name = plane.Name;
            Seat = plane.Seat;
            textOut.Write(Name + ",");
            textOut.WriteLine(Seat);
        }
    }



}

2 个答案:

答案 0 :(得分:0)

我认为您的问题的原因可能是忘记了文件的关闭,另外您可能会强制文本flush textOut.Flush()

尝试在textOut.Close()方法的末尾添加WritePiper(...)

答案 1 :(得分:0)

查看WritePiper方法。

它只读取文件,然后将其写回,不做任何更改。参数中的值将被忽略,这些参数仅在用于将数据写入文件时用作数据的临时存储。

您需要将参数中的值作为项目包含在内,方法是将其添加到列表中,或者将其与列表中的项目一起写入文件。

示例:

Seating person = new Seating();
person.Name = Name;
person.Seat = Seat;
piperSeating.Add(person);