private void button6_Click(object sender, EventArgs e)
{
string[] entries = File.ReadAllLines(@"D:\Keywords.txt");
foreach (string entry in entries)
{
string[] values = entry.Split(',');
LocalyKeyWords[values[0]].Clear();
for (int i = 1; i < values.Length; i++)
LocalyKeyWords[values[0]].Add(values[i]);
}
using (var w = new StreamWriter(@"D:\Keywords.txt"))
{
crawlLocaly1 = new CrawlLocaly();
crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
DialogResult dr = crawlLocaly1.ShowDialog(this);
if (dr == DialogResult.OK)
{
if (LocalyKeyWords.ContainsKey(mainUrl))
{
LocalyKeyWords[mainUrl].Clear();
//probably you could skip this part and create new List everytime
LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
}
else
{
LocalyKeyWords[mainUrl] = new List<string>();
LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
}
foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
{
w.WriteLine(kvp.Key + "," + string.Join(",", kvp.Value));
}
}
}
}
The error is on the line:
LocalyKeyWords[values[0]].Clear();
The error is: KeyNotFoundException: The given key was not present in the dictionary
LocalyKeyWords is a List:
Dictionary<string, List<string>> LocalyKeyWords = new Dictionary<string, List<string>>();
And the file Keywords.txt is not empty now :
http://www.cnet.com,changing for test
http://www.wlala.co.co,adding
And yet when reading the file I’m getting the exception.
I see that on the error line that LocalyKeyWords is empty 0 but values contain two indexes :
[0] http://www.cnet.com and [1] changing for test
Index 0 is the url and index 1 is the url key!
I tried to add before this line and change the code there a bit to:
if (LocalyKeyWords.Count == 0)
{
LocalyKeyWords[values[0]] = new List<string>();
}
else
{
LocalyKeyWords[values[0]].Clear();
}
But it didn’t work and it didn’t solve the problem. I’m still getting the error on the line.
How can I fix it?
The List is built this way: url,key for exmaple: http://www.google.com“,”google
also the text file format is like that:
http://www.cnet.com,changing for test
http://www.wlala.co.co,adding
changing for test is the key of http://www.cnet.com
and addin is the key for http://www.wlala.co.co
So I want to do two things when reading back the url,key from the text file:
-
To add the url and its belonging key to a
ListBoxso I did it in the constructor. I want to do it in the button click event each time the user change a key for a url or add a new url and its key so the user will see in real time the changes in ListBox. In the constructor I’m adding the url and keys and show it in the ListBox like this:sr = new StreamReader(@”d:\Keywords.txt”);
while (null != (line = sr.ReadLine()))
{index = line.IndexOf(","); key_of_each_line = line.Substring(0, index); value_of_each_key = line.Substring(index + 1); listBox1.Items.Add("Url: " + key_of_each_line + " --- " + "Key: " + value_of_each_key ); } sr.Close();
sr is a StreamReader.
And it’s working good in the constructor. The same format in the constructor. I want to show it in real time in the button click event when the user add new/change a key/s or url/s
- To fix the reading part as it is now in the button click event with exception error.
The new reading part code:
if (File.Exists(@"d:\Keywords.txt"))
{
string[] entries = File.ReadAllLines(@"D:\Keywords.txt");
foreach (string entry in entries)
{
string[] values = entry.Split(',');
if (LocalyKeyWords.ContainsKey(values[0]))
LocalyKeyWords[values[0]].Clear();
else
LocalyKeyWords[values[0]] = new List<string>();
for (int i = 1; i < values.Length; i++)
LocalyKeyWords[values[0]].Add(values[i]);
}
}
Added also a check if the file exist in case the user deleted it. But if the file exist I don’t want that each time the user click the button it will create a new empty text file.
Does doing this sort out the issue:
Alternatively, if you can reassign
LocalyKeyWordsyou could do this:The latter method is preferable as it is fully rebuilding the dictionary and therefore there is no way that there could be existing keys.