I'm trying to figure out if the text URL that I get from current URL exists in 'linkx.txt', if it does then show a message, if it doesn't then write to text file. however, when I run this code program writes to text file twice before recognizing the text exists.
[working code]
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
string linkx = @"Desktop\linkx.txt";
string url = "";
if (keyData == Keys.F1) { Application.Exit(); return true; }
else if (keyData == Keys.F2) { url = webBrowser1.Url.AbsoluteUri; return true; }
using (StreamReader sr = File.OpenText(linkx))
{
string texxt = url;
string[] lines = File.ReadAllLines(linkx);
bool matched = false;
for (int x = 0; x < lines.Length; x++)
{
if (texxt == lines[x])
{
sr.Close();
MessageBox.Show("there is a match");
matched = true;
}
}
if (!matched)
{
sr.Close();
using (StreamWriter wriite = File.AppendText(linkx))
{
wriite.WriteLine(url);
MessageBox.Show("url copied!");
return true; // indicate that you handled this keystroke
}
}
}
// Call the base class
return base.ProcessCmdKey(ref msg, keyData);
}