关于StreamWriter的菜鸟问题

jknight 2003-10-10 12:53:32
private void Form1_Load(object sender, System.EventArgs e)
{
File.CreateText("c:\\aaa.txt");
}

private void button2_Click_1(object sender, System.EventArgs e)
{
StreamWriter sw=new StreamWriter("c:\\aaa.txt");
}

出错提示:
“该进程无法访问文件“c:\aaa.txt”,因为该文件正由另一进程使用。”
怎么会这样啊。。。。。
...全文
49 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
brightheroes 2003-10-10
  • 打赏
  • 举报
回复
csharplove(亡灵巫师) ( ) 说得对,用完之后要关掉sw.close()
liqianhong 2003-10-10
  • 打赏
  • 举报
回复
//这样用
StreamWriter sw;

sw=File.CreateText("c:\\aaa.txt");
sw.Write("hello");
sw.Close();

//或者
sw=new StreamWriter("c:\\aaa.txt");
sw.Write("hello");
sw.Close();
jknight 2003-10-10
  • 打赏
  • 举报
回复
File.CreateText("c:\\aaa.txt");之后怎样关闭文件?
superhood 2003-10-10
  • 打赏
  • 举报
回复
File.CreateText("c:\\aaa.txt");执行后文件打开,在执行StreamWriter sw=new StreamWriter("c:\\aaa.txt");时由于文件已打开所以报错
csharplove 2003-10-10
  • 打赏
  • 举报
回复
肯定是你上次释放文件,记得对文件操作完要释放资源,结束操作:sw.close(),是不是close记不太清楚,反正是一个释放或关闭的方法

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧