Reputation: 47
ok what im trying to do is load a text file search it for a certain string if the string exists then it will will disable button
if it don't exist it will Wright to the file
if (File.Exists(hostfile))
{
{
string s = "elfenliedtopfan5 programupdate";
string file = hostfile;
List<string> lines = new List<string>(System.IO.File.ReadAllLines(file));
int index = lines.FindLastIndex(item => item.Contains("elfenliedtopfan5 programupdate"));
if (index != -1)
{
lines.Insert(index + 1, s);//""
}
// System.IO.File.WriteAllLines(file, lines);
MessageBox.Show("text test 1 found");
}
}
else
{
DialogResult dialogResult = MessageBox.Show("text not found would you like to add it ", "Text Not Found", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
using (StreamWriter sw = File.AppendText(hostfile))
{
sw.WriteLine("elfenliedtopfan5 update");
sw.Close();
messagebox.show("done");
}
}
}
but even know it does exist it will add it again and im confused to why this is the case any help would be appropriated
elfenliedtopfan5
Upvotes: 0
Views: 361
Reputation: 125187
Here is a simple way to read from or write to a text file:
string filename=@"D:\file.txt";
string value="your value"
var content = System.IO.File.ReadAllText(filename);
if (content.Contains(value))
this.Button1.Enabled = false;
else
System.IO.File.AppendAllLines(filename, new string(){value} );
Upvotes: 2