C# store a string variable to a text file .txt

jayt csharp picture jayt csharp · Jun 2, 2011 · Viewed 32.9k times · Source
  1. How can i store the contents of a string variable to a text file ?

  2. How can i search in a string variable for specific text for example find if the word book is in the string?

Answer

kemiller2002 picture kemiller2002 · Jun 2, 2011

To save the file to text you can do:

System.IO.File.WriteAllText("C:\your_path\your_file", Your_contents);

To Search for something in the string:

var position = Your_string.IndexOf("Book");

If position equals -1 then what you are searching for isn't there.