<p>It seems like there should be something shorter than this:</p>

<pre><code>private string LoadFromFile(string path)
{
   try
   {
       string fileContents;
       using(StreamReader rdr = File.OpenText(path))
       {
            fileContents = rdr.ReadToEnd();
       }

       return fileContents;
   }
   catch
   {
       throw;
   }
}
</code></pre>

[{'answer_id': 42290, 'author': 'Brad Wilson', 'author_id': 1554, 'author_profile': 'https://Stackoverflow.com/users/1554', 'pm_score': 2, 'selected': False, 'text': "<p>File.ReadAllText() maybe?</p>\n\n<p>ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_mscorlib/html/4803f846-3d8a-de8a-18eb-32cfcd038f76.htm if you have VS2008's help installed.</p>\n"}, {'answer_id': 42292, 'author': 'Chris Marasti-Georg', 'author_id': 96, 'author_profile': 'https://Stackoverflow.com/users/96', 'pm_score': 3, 'selected': False, 'text': '<pre><code>string text = File.ReadAllText("c:\\file1.txt");\nFile.WriteAllText("c:\\file2.txt", text);\n</code></pre>\n\n<p>Also check out ReadAllLines/WriteAllLines and ReadAllBytes/WriteAllBytes</p>\n'}, {'answer_id': 42293, 'author': 'Jimmy', 'author_id': 4435, 'author_profile': 'https://Stackoverflow.com/users/4435', 'pm_score': 5, 'selected': True, 'text': '<p>First of all, the title asks for "how to write the contents of strnig to a text file"\nbut your code example is for "how to read the contents of a text file to a string.</p>\n\n<p>Answer to both questions:</p>\n\n<pre><code>using System.IO;\n...\nstring filename = "C:/example.txt";\nstring content = File.ReadAllText(filename);\nFile.WriteAllText(filename, content);\n</code></pre>\n\n<p>See also ReadAllLines/WriteAllLines and ReadAllBytes/WriteAllBytes if instead of a string you want a string array or byte array.</p>\n'}, {'answer_id': 42322, 'author': 'Bjorn Reppen', 'author_id': 1324220, 'author_profile': 'https://Stackoverflow.com/users/1324220', 'pm_score': 2, 'selected': False, 'text': "<p>There's no point in that exception handler.  It does nothing.  This is just a shorterned version of your code, it's fine:</p>\n\n<pre><code> private string LoadFromFile(string path)\n {\n    using(StreamReader rdr = File.OpenText(path))\n      return rdr.ReadToEnd();\n }\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/42286', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3475/']