[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1286
  • Last Modified:

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)
        {

        }
    }
}
0
IzzyTwinkly
Asked:
IzzyTwinkly
  • 5
  • 4
1 Solution
 
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 ThakurCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Anurag ThakurCommented:
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 ThakurCommented:
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 ThakurCommented:
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
 
IzzyTwinklyAuthor Commented:
Thanks raqi0017.  I just finished my first xml serialization!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now