-2

I'm wondering how could I save the info I have in my listbox and create one text file with that info and retrieve that information back to the listbox when form1 opens in the next session?

I have tried this but it didnt work.

private void Form1_Closing(object sender, EventArgs e)
{
    Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
}

void Application_ApplicationExit(object sender, EventArgs e)
{
    Properties.Settings.Default.Save();
}
Saca
  • 10,355
  • 1
  • 34
  • 47
mrmoog
  • 1
  • 2
  • Why not you just call Properties.Settings.Default.Save(); in your Form_Closing() method? – VDN Jul 21 '17 at 13:37
  • @VDN I have tried that but somehow it didnt worked,I have configured settings too,can you give me a small code sample so I can see If im doing anything wrong? Thanks – mrmoog Jul 21 '17 at 13:39
  • See this : https://stackoverflow.com/questions/2890271/how-to-save-a-liststring-on-settings-default – PaulF Jul 21 '17 at 13:49
  • Possible duplicate of [How to save a List on Settings.Default?](https://stackoverflow.com/questions/2890271/how-to-save-a-liststring-on-settings-default) – PaulF Jul 21 '17 at 13:51
  • I have tried all this,sorry for the delay but this has been quite a fight,I cant save the list,I have tried a Multi line textbox and now a richtextbox wich I have tried to export the data like this: – mrmoog Jul 28 '17 at 13:12
  • 'private void btnOpen_Click(object sender, EventArgs e) { } public void SaveMyFile() { – mrmoog Jul 28 '17 at 13:12
  • var saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Text Files (*.txt)|*.txt|RTF Files (*.rtf)|*.rtf"; saveFileDialog.AddExtension = true; if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var extension = System.IO.Path.GetExtension(saveFileDialog.FileName); if(extension.ToLower()==".txt") /*saveFileDialog.FilterIndex==1*/ txtarea.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText); else txtarea.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.RichText); }' – mrmoog Jul 28 '17 at 13:12
  • txtarea = richtextbox1 – mrmoog Jul 28 '17 at 13:13

2 Answers2

0

You can use the XML serializer to save the list in XML format

/// <summary>
/// Writes the collection to the specified Xml file
/// </summary>
/// <typeparam name="T">type of the object which collection contains</typeparam>
/// <param name="objectToSave"></param>
/// <param name="path">xml file path</param>
public static void WriteXml<T>(this ICollection<T> objectToSave, string path) where T : class
{
    // create an Xml Writer
    var writer = new System.Xml.Serialization.XmlSerializer(objectToSave.GetType());

    System.IO.StreamWriter file = new System.IO.StreamWriter(path);
    // Serialize the data
    writer.Serialize(file, objectToSave);

    // close the file
    file.Close();
}

public static ICollection<T> ReadXml<T>(this ICollection<T> objectToUse, string path) where T : class
{
    try
    {
        // create the reader
        var reader = new System.Xml.Serialization.XmlSerializer(objectToUse.GetType());

        System.IO.StreamReader file = new System.IO.StreamReader(path);

        // Deserialize the data
        var data = reader.Deserialize(file);
        file.Close();

        if (data != null)
            return data as ICollection<T>;
    }
    catch (FileNotFoundException)
    {

    }
    return objectToUse;
}
FortyTwo
  • 2,414
  • 3
  • 22
  • 33
Tinu
  • 197
  • 2
  • 9
0

Something like this:

        private void Form1_Load(object sender, EventArgs e)
    {
        if (File.Exists("c:\\temp\\myfile.txt"))
        {
            var txtLines = File.ReadAllLines("c:\\temp\\myfile.txt"); 
            listBox1.Items.AddRange(txtLines);
        }
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        var sb = new StringBuilder();

        foreach (var i in listBox1.Items)
        {
            sb.AppendLine(i.ToString());
        }

        File.WriteAllText("c:\\temp\\myfile.txt", sb.ToString());
    }
VDN
  • 717
  • 4
  • 12
  • I have tried that now,it doesnt write anything to the file – mrmoog Jul 21 '17 at 14:06
  • what do you have in your listbox? – VDN Jul 21 '17 at 14:10
  • My listbox has numbers that it get's from one textbox. When I click to add the numbers to listbox it add's correcly but when I close the form it doesn't save anything in the text file. thanks for your time – mrmoog Jul 21 '17 at 14:24
  • sorry for the silly quiestion, but are you sure you call these methods at all? – VDN Jul 21 '17 at 14:28