读/写问题C#

时间:2012-12-08 08:56:55

标签: c# split streamreader

我花了几个小时的研究试图找到一个适合我的项目但没有运气的解决方案。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CustomerApplication
{
    public partial class ListAllCustomersForm : Form
    {   // default constructor method

    public ListAllCustomersForm()
    {
        InitializeComponent();
        /* Since this is the first method to be executed
         * call the displayCustomers method to populate
         * the list */


        displayCustomers();
    } // End of constructor

    // okButton Click method
    private void okBtn_Click(object sender, EventArgs e)
    {
        // retrieve hidden form's memory address
        Form myParentForm = CustomerAppStart.getParentForm();
        // now that we have the address use it to display the parent Form
        myParentForm.Show();
        //Finally close this form
        this.Close();
    } // end of okButton Click method
    //display the customers
    public void displayCustomers()
    {


        //format the header in the text box
        customerList.Text = ""; //Clear the textbox
        customerList.AppendText(
        "-------------------------------------------------- \r\n");
        customerList.AppendText(
        "Name Address Zip \r\n");
        customerList.AppendText(
        "-------------------------------------------------- \r\n");
        //local variables
        String name = "";
        String address = "";
        int zip = 0;
        //get all customers
        Customer[] myCustArray = Customer.getAllCustomers();
        //loop thru the array and display all the data


        for (int k = 0; k < myCustArray.Length; k++)
        {

            if (myCustArray[k] != null)
            {
               StreamWriter Writer = new StreamWriter(@"C:\Users\Jake\Documents\college\CIS340\data.csv", true);

                 Writer.Write(myCustArray[k].getCustomerName() + "," + myCustArray[k].getCustomerAddress() + "," + myCustArray[k].getCustomerZip() + ",");
                 Writer.Close();

                 StreamReader Reader = new StreamReader(@"C:\Users\Jake\Documents\college\CIS340\data.csv");
                 Reader.ReadToEnd();

                //get the customer attributes for this customer
                name = myCustArray[k].getCustomerName();
                address = myCustArray[k].getCustomerAddress();
                zip = myCustArray[k].getCustomerZip();
                //format the data
                name = name.PadRight(20, ' ');
                address = address.PadRight(23, ' ');
                //display it to the textBox
                this.customerList.AppendText(name + " " +
                address + " " + zip + "\r\n");
            }//end if not null

        }//end for loop
    }//end displayCustomers method


} // end of class definition
} // end of namespaces

这段代码允许我格式化我的文本框并给出我创建的标题的数组。它还允许我将“AddCustomerForm.cs”中的任何用户输入写入.CSV文件,我可以在本地检查并查看值是否已添加。

我遇到的问题是从我的.CSV文件中读取并将文本显示在格式化的文本框中。

无论我尝试什么,我能看到的唯一输出是空白格式化表单,除非我在同一个调试会话期间尝试添加新客户,在这种情况下,它将正确显示用户的输入“AddCustomerForm.cs”。

再一次,如果这看起来像是一个新手问题,我很抱歉,我向你保证,我已经研究了一段时间,但没有明确的解决方案。我非常感谢任何人都可以提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

您没有使用Reader.ReadToEnd()调用的结果只是读取它然后去除它们。

尝试使用StreamReader的ReadLine方法读取行。