I have this button click event:
private void button6_Click(object sender, EventArgs e)
{
if (File.Exists(@"d:\Keywords.txt"))
{
Dictionary<string,string> test = new Dictionary<string,string>();
string value_of_each_key;
string key_of_each_line;
string line;
int index;
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);
test.Add(key_of_each_line, value_of_each_key);
}
sr.Close();
}
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 reading of the text file is working good ( did it for a test for now and its working good ).
Thep roblem is that each time i click the button it will also create a new text file and what i want is that when i click the button the text file will be ready to add a new text to him and not ot create each time a new one.
How can i solve that ?
It sounds like you just want to change this:
To this:
That will use the overload of the
StreamWriterconstructor which has a second parameter controlling overwrite/append behaviour.Alternatively, and more readably, use
File.AppendText: