title: System.UnauthorizedAccessException was unhandled
body: <p>I am getting a access denied exception. How can I fix this?</p>
  
  <p>Here is the exception:</p>
  
  <blockquote>
    <p>System.UnauthorizedAccessException was unhandled   HResult=-2147024891
    Message=Access to the path 'c:\message.txt' is denied.<br>
    Source=mscorlib</p>
  </blockquote>
  
  <p>Here is the code:</p>
  
  <pre><code>    public static void WriteToFile(string s)
      {
          fs = new FileStream("c:\\message.txt",
          FileMode.Append, FileAccess.Write);
          sw = new StreamWriter(fs);
          sw.WriteLine(s);
          sw.Flush();
          sw.Close();
          fs.Close();
      }
  </code></pre>
  
  <p>EDIT: It works if I run vs2012 as administrator, but is there a way or a reason to do it as normal user?</p>
  
  <p>And this works:</p>
  
  <pre><code>    public static void WriteToFile(string s)
      {
          fs = new FileStream(@"C:\Users\KristjanBEstur\Documents\message.txt",
          FileMode.Append, FileAccess.Write);
          sw = new StreamWriter(fs);
          sw.WriteLine(s);
          sw.Flush();
          sw.Close();
          fs.Close();
          File.Delete(@"C:\Users\KristjanBEstur\Documents\message.txt");
      }
  </code></pre>