I store some string values in an array list at runtime. When the application closes I want to store the data from the array list in another location so that I can open and retrieve the data the next time the application starts. What are some possible ways to do this?
Asked
Active
Viewed 632 times
1
-
You really shouldn't ever use `ArrayList` anymore. You should use `List
` so that you have a strongly typed collection. – Servy Jul 24 '12 at 17:11
2 Answers
1
You can use Reading
and Writing
from/to files or storing the values in windows registry.
For Reading/Writing from/to files use:
StreamReader sr = new StreamReader(@"C:/store.dll"); //for reading
StreamWriter sw = new StreamWriter(@"C:/store.dll"); //for writing
This is the basic. Here are two great articles for this:
I used this for storing the High Scores for a simple game. :)
Here is a nice tutorial for using Windows registry.

Ionică Bizău
- 109,027
- 88
- 289
- 474
-
1
-
-
Is there any different, more "isolate" way, to save and restore my data? I prefer no to use window registry – user1005633 Jul 24 '12 at 17:13
-
-
If i haven't a D disk partition it stored somewhere else, or it doesn't stored? How can i know what drive letters has every pc? :-) – user1005633 Jul 24 '12 at 17:22
-
This can help you: http://stackoverflow.com/questions/5047570/c-sharp-how-to-know-if-a-given-path-represents-a-root-drive :D – Ionică Bizău Jul 24 '12 at 17:26
0
You could Serialize
the ArrayList and write it to disk, then later on load the file and Deserialize
it to an object:
public static XDocument Serialize<T>(T objectIn, Type[] extraTypes)
{
try
{
var target = new XDocument();
XmlSerializer s = extraTypes != null ? new XmlSerializer(objectIn.GetType(), extraTypes) : new XmlSerializer(objectIn.GetType());
s = extraTypes != null
? new XmlSerializer(objectIn.GetType(), extraTypes)
: new XmlSerializer(objectIn.GetType());
var writer = target.CreateWriter();
s.Serialize(writer, objectIn);
writer.Close();
return target;
}
catch (Exception ex)
{
throw new Exception(string.Format("Could not serialize object: {0}", ex.Message));
}
}
public static T Deserialize<T>(XDocument xDocument, string defaultNamespace)
{
XmlSerializer s = new XmlSerializer(typeof(T), defaultNamespace);
T result = (T)s.Deserialize(xDocument.CreateReader());
return result;
}

paul
- 21,653
- 1
- 53
- 54
-
How can i call this method? I suppose T objectIn is my arraylist, what is the Type[] extraTypes – user1005633 Jul 24 '12 at 17:10