How can i write the values in the List to a text file and then read back from the text file to the L

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I added in the Top of Form1:

<pre class="prettyprint StreamWriter w;[/code]
In the constructor i did:

<pre class="prettyprint w = new StreamWriter(@"d:Keywords.txt");[/code]
Then i have this button click event:

<pre class="prettyprint private void button6_Click(object sender, EventArgs e)
{
crawlLocaly1 = new CrawlLocaly();
crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
DialogResult dr = crawlLocaly1.ShowDialog(this);
if (dr == DialogResult.Cancel)
{
crawlLocaly1.Close();
}
else if (dr == DialogResult.OK)
{
//LocalyKeyWords.Add(crawlLocaly1.getText() + "," + mainUrl);
if(LocalyKeyWords.ContainsKey(mainUrl))
{
LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());


}
else
{
LocalyKeyWords.Add(mainUrl, new List<string>(new string[]
{
crawlLocaly1.getText()
}
));
}
crawlLocaly1.Close();
}

int count = 0;
foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
{
w.WriteLine(count.ToString() + " " + kvp.Key + " " + kvp.Value); count++;
}
w.Close();
}[/code]
The List LocalyKeyWords is: <string,List<string>> type of Dictionary.
Once the user type in the textBox its going to the else part and add to the List a key to the url second time the user add a new key for the same url it iwll go to the first part of the if and will change the key to the same url. If the user first changed
the url when the program is running so it iwll go to the else part and add a keyword to the new url.
So if im using a breakpoint on the List i will see for example in index 0:
http://www.google.com,google
the left side is the url address the right side is the key(kyword).

Now the event is working but now i want to write the List values to a text file so the text file format iwll be like this:

http://www.google.com,google
http://www.cnet.com,hello
http://www.microsoft.com,Daniel
Then i want to read back this values from the text file and put them back in a listBox when im running the program in the constructor so in the listBox the format will be like:
Url: http://www.google.com http://www.google.com --- Key: google
Url: http://www.cnet.com http://www.cnet.com --- Key: hello
Url: http://www.microsoft.com http://www.microsoft.com --- Key: Daniel
The problem is when im writing now to the text file as the code is in the button click event the result in the text file is:

0 http://www.google.co.il http://www.google.co.il System.Collections.Generic.List`1[System.String]
Its not a but but its not the format i wanted in the text file. I wanted it to be:
http://www.google.com,google
http://www.cnet.com,hello
http://www.microsoft.com,Daniel
Second problem when im adding a new keyword while the program is running im getting error exception on the line:

<pre class="prettyprint w.WriteLine(count.ToString() + " " + kvp.Key + " " + kvp.Value);[/code]
Cannot write to a closed TextWriter.

So the things i need:

1. To write to the text file the values in the format i mentioned above.
2. To read from the text file in the constructor directly to a listBox in the format i mentioned above.

Thank you very much.
<hr class="sig danieli

View the full article
 
Back
Top