2016-04-30 77 views
0

我正在申請一個'滑雪勝地',您可以在其中添加成員和搜索成員等等。到目前爲止,我在應用程序關閉時無法保存和加載這些應用程序。當我添加一個成員時,它會保存,然後當我嘗試添加另一個成員時,第一個成員將不再保存,但第一個成員已經完成。 這裏是用來保存成員的代碼:麻煩保存C#WPF

 public void Save(System.IO.TextWriter textOut) 
    { 
     textOut.WriteLine(number); 
     textOut.WriteLine(name); 
     textOut.WriteLine(address); 
     textOut.WriteLine(score); 
     textOut.Close(); 
    } 

    public bool Save(string filename) 
    { 
     System.IO.TextWriter textOut = null; 
     try 
     { 
      textOut = new System.IO.StreamWriter(filename); 
      Save(textOut); 
     } 
     catch 
     { 
      return false; 
     } 
     finally 
     { 
      if (textOut != null) 
      { 
       textOut.Close(); 
      } 
     } 
     return true; 
    } 

回答

3

您使用每次都會覆蓋該文件StreamWriter構造函數。使用也需要布爾值的構造函數,以便可以將其添加到文件中。而且你不需要TextWriter

try 
{ 
    using(var textOut = new System.IO.StreamWriter(filename, true)) 
    { 
     textOut.WriteLine("whatever.."); 
    } 
}