我想从streamwriter的内容中保存文件,但是在这段代码中
SaveFileDialog savefile = new SaveFileDialog();
savefile.FileName = "unknown.txt";
savefile.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*|";
if (savefile.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(savefile.FileName, false, System.Text.Encoding.Unicode))
sw.WriteLine("Test line");
sw.WriteLine("Test line2");
sw.WriteLine("Test line3");
}
在行中sw.WriteLine("Test line2");
sw.WriteLine("Test line3");
,是错误,sw
不存在!
但我很少使用代码
using (StreamWriter sw = new StreamWriter("\unknow.txt", false,System.Text.Encoding.Unicode))
sw.WriteLine("Test line");
sw.WriteLine("Test line2");
sw.WriteLine("Test line3");
一切正常!问题出在哪里?谢谢 !