To read i tried this code:
In the top of Form1 i did:
Dictionary<string, List<string>> LocalyKeyWords = new Dictionary<string, List<string>>();
In the constructor i did:
keywords = @"d:\Keywords.txt";
if (File.Exists(keywords))
{
LoadKeys(LocalyKeyWords, keywords);
}
The function LoadKeys:
private void LoadKeys(Dictionary<string,List<string>> disctionary, string FileName)
{
var lines = File.ReadAllLines(keywords).Select(l => l.Split(','));
var dict = new Dictionary<string, List<string>>();
foreach(var splits in lines)
{
var key = splits.First();
var value = splits.Skip(1).ToList();
try {dict.Add(key, value);
}
catch(Exception ex)
{ } }
}
No exceptions but in the constructor after calling the function and all is done the LocalyKeyWords is empty.
This is how im writing to the file today the keys and values:
private void button6_Click(object sender, EventArgs e)
{
using (var w = new StreamWriter(keywords))
{
crawlLocaly1 = new CrawlLocaly();
crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
DialogResult dr = crawlLocaly1.ShowDialog(this);
if (dr == DialogResult.OK)
{
if (LocalyKeyWords.ContainsKey(mainUrl))
{
LocalyKeyWords[mainUrl].Clear();
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));
}
}
}
}
Maybe now you will be able to solve the loading the keys and values in the constructor and also to solve the problem how to write each time im doing a change of the keys or values in the button6 click event.
You should probably wrap that in a try catch as well.