It seems like there should be something shorter than this:
private string LoadFromFile(string path) { try { string fileContents; using(StreamReader rdr = File.OpenText(path)) { fileContents = rdr.ReadToEnd(); } return fileContents; } catch { throw; } }
First of all, the title asks for ‘how to write the contents of strnig to a text file’ but your code example is for ‘how to read the contents of a text file to a string.
Answer to both questions:
See also ReadAllLines/WriteAllLines and ReadAllBytes/WriteAllBytes if instead of a string you want a string array or byte array.