Chaos Project

Game Development => Sea of Code => Topic started by: G_G on October 12, 2009, 02:49:27 pm

Title: Loading and Saving Arrays into Text(Resolved)
Post by: G_G on October 12, 2009, 02:49:27 pm
Well I got the loading an array from text, it loads every line and pops it into an array. Now my question is how would I go through every thing in the array and save it into a text file? I know how to write to a text file like this.
StreamWriter tw = new StreamWriter(file);
tw.WriteLine("text");
tw.Close();

Now how to I go through every item in my array? So like I have this array urls which holds the users bookmarks, so when clicked it'll navigate to that website. Now it works and all but I need it so when people add or remove a bookmark it updates the file that holds all the data. So any idea?
Title: Re: Loading and Saving Arrays into Text
Post by: Blizzard on October 12, 2009, 02:56:04 pm
StreamWriter tw = new StreamWriter(file);
foreach (string line in array)
{
   tw.WriteLine(line + "\n");
}
tw.Close();
Title: Re: Loading and Saving Arrays into Text
Post by: Ryex on October 12, 2009, 03:00:33 pm
the +"\n" is unnecessary each time the WriteLine Method is called it automatically writes to a new line
Title: Re: Loading and Saving Arrays into Text
Post by: Blizzard on October 12, 2009, 03:01:33 pm
Right. Well noticed. And even if it was Write(), it had to be "\r\n". That was the Ruby in me up there. xD
Title: Re: Loading and Saving Arrays into Text
Post by: G_G on October 12, 2009, 03:08:10 pm
okay now how would I do it for every item in the combobox? I tried what blizz said in the c# help topic, but I'm not sure how to get it.
not sure what to do so thanks guys for the help.
Title: Re: Loading and Saving Arrays into Text
Post by: Blizzard on October 12, 2009, 03:18:01 pm
StreamWriter tw = new StreamWriter(file);
foreach (object item in combobox.Items)
{
   tw.WriteLine((string)item);
}
tw.Close();


You need to cast it into a string since all items in the combobox are cast to object.
Title: Re: Loading and Saving Arrays into Text(Resolved)
Post by: G_G on October 12, 2009, 03:20:43 pm
Thanks guys it works all right now *marks as resolved*