my question is why I am unable to acces local variable sw here... I am attempting to write text to a file to save it but the sw.WriteLine(element); in the foreach loop is unable to use sw.
public void SaveToFile(List<string>lines)
{
FileStream fs;
StreamWriter sw;
try
{
fs = new FileStream(fileName,
FileMode.OpenOrCreate, FileAccess.Write);
sw = new StreamWriter(fs);
}
catch (IOException)
{ MessageBox.Show("Error opening file"); }
/////////////////////////////////////////////////////////////////////////
try
{
foreach (string element in lines)
{
sw.WriteLine(element);
}
}
catch (IOException)
{ MessageBox.Show("Error writing to file"); }
////////////////////////////////////////////////////////////////////////
try
{
if (sw != null)
sw.Close();
}
catch (IOException)
{ MessageBox.Show("Error closing file"); }
}