Loading and Saving Arrays into Text(Resolved)

Started by G_G, October 12, 2009, 02:49:27 pm

Previous topic - Next topic

G_G

October 12, 2009, 02:49:27 pm Last Edit: October 12, 2009, 03:20:32 pm by game_guy
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?

Blizzard

StreamWriter tw = new StreamWriter(file);
foreach (string line in array)
{
   tw.WriteLine(line + "\n");
}
tw.Close();
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Ryex

the +"\n" is unnecessary each time the WriteLine Method is called it automatically writes to a new line
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

Right. Well noticed. And even if it was Write(), it had to be "\r\n". That was the Ruby in me up there. xD
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

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.

Blizzard

October 12, 2009, 03:18:01 pm #5 Last Edit: October 12, 2009, 03:19:03 pm by Blizzard
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.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

Thanks guys it works all right now *marks as resolved*