FileStream 读写文件
读取正常,写入提示文件正在使用(同一个文件打开,编辑之后保存提示文件正在使用)using 不是已经释放掉资源了吗?求大神大神大神来指教
//读取文件
try
{
using (FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read))
{
using (StreamReader sr = new StreamReader(fs))
{
ofd.OpenFile(); //打开文件
string line = sr.ReadLine(); //读取文本行
SqlText.Text = "";
while (line != null)
{
SqlText.Text += line + "\n";
line = sr.ReadLine();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
//写入文件
try
{
using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(SqlText.Text);
sw.Flush();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}