title: how can i write to text file out of scope it is defined in?
body: <p>How can i write to a text file outside of the scope it is created in?</p>
  
  <p>say for example my code looks a bit like this:</p>
  
  <pre><code>try
  {
      StreamWriter file = new StreamWriter(path);
  }
  catch (NullReferenceException) //unable to create file
  {
      MessageBox.Show("Cannot create file");
      //end program
  }
  
  file.WriteLine("hello world!") //error at compile time here 
  //                  "The name 'file' does not exist in current context"
  </code></pre>
  
  <p>if the file cannot be created the program is ended straight away
  can i do this or not?</p>