2016-08-05 4 views
0

私は製品を保存して読み込むアプリケーションを作っています。これらの製品には、製品名、顧客名、およびファームウェアの場所の3つのプロパティがあります。しかし、私がそれらを保存しようとすると、それは1つだけを保存し、最新の製品を保存して上書きし続けます。製品クラスのコードは次のとおりです。保存はそれ自身を上書きし続けますC#

public class Product 
{ 

    //private product data 
    private string productName; 

    public string getProductName() 
    { 
     return this.productName; 
    } 

    public void setProductName (string inProductName) 
    { 
     this.productName = inProductName; 
    } 

    private string customerName; 

    public string getCustomerName() 
    { 
     return this.customerName; 
    } 

    public void setCustomerName (string inCustomerName) 
    { 
     this.customerName = inCustomerName; 
    } 

    private string firmwareLocation; 

    public string getFirmwareLocation() 
    { 
     return this.firmwareLocation; 
    } 

    public void setFirmwareLocation (string inFirmwareLocation) 
    { 
     this.firmwareLocation = inFirmwareLocation; 
    } 


    //constructor 
    public Product (string inProductName, string inCustomerName, string inFirmwareLocation) 
    { 
     productName = inProductName; 
     customerName = inCustomerName; 
     firmwareLocation = inFirmwareLocation; 
    } 


    //save method 
    public void Save (System.IO.TextWriter textOut) 
    { 
     textOut.WriteLine(productName); 
     textOut.WriteLine(customerName); 
     textOut.WriteLine(firmwareLocation); 
    } 

    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; 
    } 

最後に保存方法があります。ここで

は、ユーザーがアドオン製品のボタンを押したときのためのコードです:

private void Add_Click(object sender, RoutedEventArgs e) 
    { 
     //get input from user 
     string inputCustomerName = customerNameTextBox.Text; 
     string inputProductName = productNameTextBox.Text; 
     string inputFirmwareLocation = firmwareTextBox.Text; 

     try 
     { 
      Product newProduct = new Product(inputProductName, inputCustomerName, inputFirmwareLocation); 
      newProduct.Save("products.txt"); 
      MessageBox.Show("Product added"); 
     } 
     catch 
     { 
      MessageBox.Show("Product could not be added"); 
     } 
    } 
+0

はい、それは上に書いたファイルに追加しないでください – BugFinder

答えて

2

あなたがあなたのファイルにテキストを追加していないことが何度も何度も最後のエントリを上書きし続ける理由を、thatsの。 StreamWriterコンストラクタで2番目のパラメータとして「真」

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

お知らせ:

はあなたの保存方法に変更してみてください。これは、新しい行を追加するようにStreamWriterに指示します。

+0

ありがとうございました – lucycopp

+0

それがあなたを助けたなら答えを受け入れることを忘れないでください;) –

関連する問題