0
私は初めて自分でコードを作成しようとしており、Winformsで音楽プレーヤーを作ることに決めました。CheckedListBox選択をテキストファイルに送る方法は?
私はすでにフォルダ内の曲の名前が移入さにCheckedListBoxを持っています。 ボタンをクリックすると、フォームを閉じる前に、選択した曲の名前を.txtファイルに送信してさらに操作できます。簡略化のため
、私は最初の1つの選択された歌で行きますよ。私streamreader/writer
クラスの
private void selectbtn_Click(object sender, EventArgs e)
{
selectedSongs = checkedListBox1.CheckedItems.ToString();
songRecord.writeRecord(selectedSongs); //i initialised my streamreader/streamwriter class and called it songRecord
this.Close();
}
、これは私がそれを実行すると、.txtファイルは私の選択が表示されない
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();
}
}
}
を持っているものです。それだけを示しています。 System.Windows.Forms.CheckedListBox+CheckedItemCollection
を何が悪かったのか?
[設定]を保存するにCheckedListBox項目](https://stackoverflow.com/q/32773280/3110834) –