2017-05-29 20 views
5

私は音楽プレーヤーを作っています。 2つの形式があります。 1つはあなたが音楽を演奏する主要な領域です。 2番目のフォームには、必要なmp3を選択するCheckedListBoxがあります。 ボタンをクリックすると、その選択が.txtファイルに保存されるので、最初のフォームでそのファイルにアクセスできます。ここで、ファイルを見つけるために音楽プレーヤーのパスに文字列を挿入します。ファイルが既に存在する場合にファイルを上書きする方法

これは私の2番目のフォームのコードで、選択した曲を.txtファイルに保存します。

private void selectbtn_Click(object sender, EventArgs e) 
{ 
    if (File.Exists(@"C:\Users\Me\Desktop\JAM_MACHINE\JAMS\record.txt")) 
    { 
     File.WriteAllText(@"C:\Users\Me\Desktop\JAM_MACHINE\JAMS\record.txt", String.Empty); 
    } 

    string[] checkedtitles = new string[checkedListBox1.CheckedItems.Count]; 

    for (int ii = 0; ii < checkedListBox1.CheckedItems.Count; ii++) 
    { 
     checkedtitles[ii] = checkedListBox1.CheckedItems[ii].ToString(); 
    } 
    string selectedSongs = String.Join(Environment.NewLine, checkedtitles); 

    songRecord.writeRecord(selectedSongs); //I initialised the class containing streamwriter/reader, and called it songRecord 
    this.Close(); 
} 

問題は、私は、プログラムを閉じて、もう一度それを開くたびに、私は.txtファイルを消去/書き換えができない、です。既存のファイルに追加するだけです。私が正しいことをしていないことはありますか?

ここは私のストリームリーダ/ライターコードです。私はあまりにも実行した後、それを閉じてかなり確信している、おそらく誰かが間違っているかを把握することができます

namespace songss 
{ 
    class DataRecord 
    { 
    public void writeRecord(string line) 
    { 
     StreamWriter sw = null; 
     try 
     { 
      sw = new StreamWriter(@"C:\Users\Me\Desktop\JAM_MACHINE\record.txt", true); 
      sw.WriteLine(line); 
     } 
     catch (FileNotFoundException) 
     { 
      Console.WriteLine("Error: File not found."); 
     } 
     catch (IOException) 
     { 
      Console.WriteLine("Error: IO"); 
     } 
     catch(Exception) 
     { 
      throw; 
     } 
     finally 
     { 
      if (sw != null) 
       sw.Close(); 
     } 
     } 

public void readRecord() 
    { 
     StreamReader sr = null; 
     string myInputline; 
     try 
     { 
      sr = new StreamReader(@"C:\Users\Me\Desktop\JAM_MACHINE\record.txt"); 
      while ((myInputline = sr.ReadLine()) != null) ; //readline reads whole line 
      Console.WriteLine(myInputline); 
     } 
     catch (FileNotFoundException) 
     { 
      Console.WriteLine("Error: File not found"); 
     } 
     catch(IOException) 
     { 
      Console.WriteLine("Error: IO"); 
     } 
     catch (Exception) 
     { 
      throw; 
     } 
     finally 
     { 
      if (sr != null) 
       sr.Close(); 
     } 
    } 
    } 
    } 
+2

ファイルが存在する場合は、単に削除しないでください。 – JohanP

+2

あなたの 'songRecord.writeRecord'は何をしていますか?ファイルを消去する前にファイルを開いている場合は、既に現在のテキストが読み込まれている可能性があります。 – Rob

+0

APIドキュメントによると、 'File.WriteAllText'は内容を置き換えるべきです。そうでない場合は、バグレポートを提出してください。 –

答えて

11

File.WriteAllTextは、あなたが欲しいものを行う必要があります。

新しいファイルを作成し、指定された文字列をファイルに書き込み、次に がファイルを閉じます。ターゲットファイルがすでに存在する場合は上書きされます。

StreamWriter classも追加/上書きするオプションがあります:

は、デフォルトのエンコーディングおよびバッファサイズを使用して、指定 ファイルのStreamWriterクラスの新しいインスタンスを初期化します。ファイル が存在する場合は、上書きまたは追加することができます。

public StreamWriter(
    string path, 
    bool append 
) 

例:

using (StreamWriter writer = new StreamWriter("test.txt", false)){ 
    writer.Write(textToAdd); 
} 

あなたは追加意味 trueに渡している、あなたのコードを見てみます。

sw = new StreamWriter(@"C:\Users\Me\Desktop\JAM_MACHINE\record.txt", true); 
sw.WriteLine(line); 
1

ニコラスが正しい。 StreamWriterをfalseに変更して問題を解決しました。私はFile.WriteAllTextのどちらかを使う必要はありませんでした。それをfalseとして渡すことは追加できません。

ありがとうございました!

0

使用この

File.WriteAllText(@"C:\Users\Me\Desktop\JAM_MACHINE\JAMS\record.txt", line); 

代わりの

sw = new StreamWriter(@"C:\Users\Me\Desktop\JAM_MACHINE\record.txt", true); 
      sw.WriteLine(line); 
関連する問題