0
我想第一次自己編碼,並決定在Winforms上製作音樂播放器。如何將CheckedListBox選擇發送到文本文件?
我有一個已經填充了文件夾中的歌曲名稱的CheckedListBox。 當我點擊一個按鈕時,應該在關閉表單之前將所選歌曲的名稱發送到.txt文件以供進一步操作。
爲了簡化,我只是先選擇1首歌曲。
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();
}
在我streamreader/writer
類,這是我
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();
}
}
}
當我運行它,.txt文件沒有顯示我的選擇。它只顯示: System.Windows.Forms.CheckedListBox+CheckedItemCollection
出了什麼問題?
[保存CheckedListBox項目設置](https://stackoverflow.com/q/32773280/3110834) –