I am working with .txt files in C#, I want a functionality that if any text already exist in my .txt file, it should not write it. Can somebody Please help me? Thank you
Its pretty simple you can use following code for it var filename = @"c:\test.txt"; var textToWrite = "your text here"; string fileContents; // Get File Text using (var sr = new StreamReader(filename)) { fileContents = sr.ReadToEnd(); } // Write to File using (var sw = new StreamWriter(filename, true)) { // check if text already exists in file if (!fileContents.Contains(textToWrite)) { sw.WriteLine(textToWrite); // or use sw.Write depending on the usage scenario } }