i'm try to use streamwriter to save the data from a listbox (in C#) to a text file, but my code is saving the ojectcollection instead of the data from the listbox.
a) data saved to text file is "System.Windows.Forms.ListBox+ObjectCollection"
b)
FileStream dFile = new FileStream("C:\\temp\\data.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(dFile)
sw.WriteLine("{0}", listBox1.Items);
sw.Close();
C) please advise how to load data from the saved text file to a lisbox?
thanks
VulpesPosted Sep 28, 2012, 4:21 PM
FileStream dFile = new FileStream("C:\\temp\\data.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(dFile); //write data to file
foreach (object item in listBox1.Items)
{
sw.WriteLine("{0}", item.ToString());
}
sw.Close();
To load the listbox from the file:
FileStream dFile = new FileStream("C:\\temp\\data.txt", FileMode.Open);
StreamReader sr = new StreamReader(dFile); // read data from file
string item;
while ((item = sr.ReadLine()) != null)
{
listBox1.Items.Add(item);
}
sr.Close();
mengPosted Sep 29, 2012, 12:55 PM
FroglegPosted Sep 28, 2012, 4:31 PM