Saving the values in ArrayList for next time use

Hi,
I have a winform that uses an arraylist that holds each file's path.   However, the values(file pathes) in the arraylist won't be available when I close the application and reopen it.  So I treied to use Properties.Settings.Default to save the values in Arraylist(listboxContainer in here).   Then I thought that I can assign the values from this default settings(Name: Container; Type:System.Collection.Specialized.StringCollection; Scope:User) to my arraylist, listboxContainer after InitializeComponent();

is there any way I can save the values in arraylist?  so when I close my application and reopen it, I can use the values from the last time in arraylist?  In here, if I click Extract button right after open the application, the values inside my arraylist from previous should be appeared.
Please see the my code below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace DeployBackend
{
    public partial class Form1 : Form
    {
        OpenFileDialog dlg = new OpenFileDialog();
        ArrayList listboxContainer = new ArrayList();
   
        public Form1()
        {
            InitializeComponent();
            // Assign the values from default setting to listboxContainer.
        }
     

        private void button1_Click(object sender, EventArgs e)
        {
            dlg.Multiselect = true;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                foreach (string fileName in dlg.FileNames)
                {
                    textBox1.Text = fileName;                  
                    listboxContainer.Add(fileName);
                }
            }
        }

 
 
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            foreach (string item in listboxContainer)
            {
                Properties.Settings.Default.Container.Add(item);
            }
            Properties.Settings.Default.Save();
           
        }

        private void button3_Click(object sender, EventArgs e)
        {

            foreach (string item in listboxContainer)
            {
                MessageBox.Show(item);
            }
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
IzzyTwinklyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rupesh PCommented:
Why can't you save to a text file and reload it when you re-opens.
you can use it with the help of a schema file and it will act just like database table.  If you need the code to write and take the content back , pls let me know.
0
Anurag ThakurTechnical ManagerCommented:
the best approach will be not to use a arraylist but to use List<string> because then you can serialize the object to XML and save to the file on the disk on application exit and when the application is restarted you can read back the xml again and reconstruct your list back again
0
IzzyTwinklyAuthor Commented:
Both sound very interesting!: Saving in text file and using List<string>.  I am a beginner, so I don't have little knowlege about especially generic and xml.  Can you guys give me codes or some detailed examples?  I want to learn both ways.   The Points will go to both if you show me enough answers to solve this issue.  I am adding another 100 since I am a C# beginner and need kinda detailed steps.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Anurag ThakurTechnical ManagerCommented:
i have got an example of serialization and deserialization of an arraylist
http://www.dotnetfunda.com/articles/article123.aspx

one more good sample for the same
http://sharpertutorials.com/serialization/

you can use XMLDocument class to save the XML generated to the disk and then load it back
XmlDocument.Save should save the file to disk - http://msdn.microsoft.com/en-us/library/dw229a22(VS.80).aspx
XmlDocument.Load should be able to load the xml back - http://msdn.microsoft.com/en-us/library/875kz807(VS.80).aspx

in place of xmldocument class you can use IO class to achieve the same
0
IzzyTwinklyAuthor Commented:
Hi raqi0017,
If I save the file to some place in disk, I would have to assign the path in the code as your example shows, right?  Then I am just wondering if my application is running on not my machine, but some others' machines and if any of those doesn't have the path I assigned, shouldn't it be a problem?
0
IzzyTwinklyAuthor Commented:
can I just use 'StreamWriter sw = File.CreateText(path)' instead of using 'StreamWriter sw = new StreamWriter(path) '? to resolve this problem?
0
Anurag ThakurTechnical ManagerCommented:
i am assuming from the settings that you mentioned that the path that you want to save are independent for each user and for each installation
if that is the case then this kind of approach is perfect else if the paths are same and common for all the users and every one needs the same paths then the best place to save it will be the database
0
IzzyTwinklyAuthor Commented:
Yes..it was..only if I could save it using Properties.Settings.Default...Then it wouldn't be a problem since the each application in different machines will have its own values and saved in application using Properties.Settings.Default.  However, in this case, if I set the path inside the code, other users can't change that path...so that's y I asked you if I can use File.Create() method..(I wrote it CreateText but I meant Create).  Then, even if a user doesn't have the same path with the one i assigned, the file could be created.  That's what I thought.  What do you think?
0
Anurag ThakurTechnical ManagerCommented:
i think its best to save the setting in the application configuration file - a default value at least (applicationname.exe.config file). if the user changes the setting then you can update the value for that users and that instace of the installation on one machine. on othe machines the setting will remain the same (what has been set by default and is available through setup)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IzzyTwinklyAuthor Commented:
Thanks raqi0017.  I just finished my first xml serialization!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.