我这段代码是保存sb中的文件的,sb是一个stringbuild类的实例,其中已经有数据,但是保存后的txt文件里面却是空的,该怎么修改?
public void Saved()
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "文本文档(*.txt)|*.txt|文档(*.doc)|*.doc";
sfd.RestoreDirectory = true;
if(sfd.ShowDialog() == DialogResult.OK)
{
string fileName = sfd.FileName;
SaveFile(fileName);
}
}
public void SaveFile(string fileName)
{
try
{
using (FileStream fs = File.OpenWrite(fileName))
{
using (StreamWriter sw = new StreamWriter((Stream)fs,Encoding.UTF8))
{
sw.Write(sb.ToString());
}
}
}
catch (IOException ex)
{
MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
------解决方案--------------------我用你的代码,文件里面有内容
------解决方案--------------------