C#保存到文本文件中的多行

时间:2013-10-11 10:29:53

标签: c# .net file save

我一直在为大学项目做这件事并且遇到了问题。我已设法从文件加载多行,但我无法将它们保存回文件。我可以将一个字符串保存到文件中,这是最后处理的字符串,但就是这样。我可能通过执行循环完全错误,但我想不出任何其他方法来做到这一点。 savefile部分的编码如下:

    case "s":
    case "8":
        {
            int savecount = 0;
            string savestring = "";

            //Clear the text file ready to be saved
            using (FileStream fs = File.Create("billing.txt"))
            {

            }

            while (savecount != CustomerCount)
                {
                    using (StreamWriter save = new StreamWriter("billing.txt"))
                        {
                //Create the string to save to the file
                            savestring = CustomerNumber[savecount] + ","
                              + CustomerName[savecount] + ","
                              + Address[savecount] + ","
                              + RateScheme[savecount] + ","
                              + PeakKWH[savecount] + ","
                              + OffPeakKWH[savecount] + ","
                              + StandardKWH[savecount];

                            Console.WriteLine(savestring);

                            save.WriteLine(savestring);

                            savecount++;
                            Console.ReadLine();
                          }
                 }
            Console.WriteLine("All data saved successfully");
            Console.ReadLine();
            break;
        }

不知道从哪里开始。任何帮助将不胜感激

4 个答案:

答案 0 :(得分:1)

您应该在循环之前打开保存的文件。 E.g。

using (StreamWriter save = new StreamWriter("billing.txt")) {
    while (savecount != CustomerCount) { 
        // rest of your code here

目前,您正在每个循环中打开文件,写出一行。然后重新打开它(并丢失已写入的数据)。

正如评论中所指出的,您无需致电File.Create。默认情况下,StreamWriter将覆盖现有文件。

答案 1 :(得分:1)

你需要using { }内的while循环因为你每次都要覆盖你的数据,当你看到它时,将最后一项留在你的文件中:

using (StreamWriter save = new StreamWriter("billing.txt"))
{
     while (savecount != CustomerCount)
     {
       //Create the string to save to the file
       string savestring = CustomerNumber[savecount] + ","
       + CustomerName[savecount] + ","
       + Address[savecount] + ","
       + RateScheme[savecount] + ","
       + PeakKWH[savecount] + ","
       + OffPeakKWH[savecount] + ","
       + StandardKWH[savecount];

       Console.WriteLine(savestring);

       save.WriteLine(savestring);

       savecount++;
       Console.ReadLine();
   }
}

答案 2 :(得分:1)

你做错了是,你在每次迭代中打开文件,在文件中写一行,然后再次重新打开文件并覆盖内容。您可以重新更改代码

using (StreamWriter save = new StreamWriter("billing.txt")) 
{
   while (savecount != CustomerCount) 
   { 
     // rest of string formation of saveString logic and save.WriteLine(savestring); goes here
     .....
   }
}

我认为您可以使用一个简单的代码,您可以将所有输入字符串保存在List中并使用File.WriteAllLines函数作为

     {
       ....   
        List<string> Customers = new List<string>();
        for (savecount = 0; savecount < CustomerCount; savecount++)
        {            
         //Create the string to save to the file
            Customers.Add( CustomerNumber[savecount] + "," + CustomerName[savecount] + "," + Address[savecount] + "," + RateScheme[savecount] + "," + PeakKWH[savecount] + "," + OffPeakKWH[savecount] + "," + StandardKWH[savecount]);

            Console.WriteLine(Customers[savecount]);
        }

        string filePath = "billing.txt"; // This is your file path where all the contents are to be written
        File.WriteAllLines(filePath, Customers);
       ..........
     }

答案 3 :(得分:0)

你需要:

using (StreamWriter save = new StreamWriter("billing.txt")) {
    while (savecount != CustomerCount) {

你必须在循环之前打开文件,因为打开内部会删除之前写入的所有数据,也需要一些时间才能打开。

但是你可以在循环内打开文件,但你需要设置append文件,它将是:

StreamWriter save = new StreamWriter("billing.txt", true)

此选项较慢,您可能需要在以追加模式打开之前清除文件,因此它不是最佳选择。