作一个WinForm程序
目录不存在文件名为1.txt
首先WinForm加载,如果不存在文件1.txt那么就创建:private bool InitData(string filePath)
{
if (!File.Exists(filePath))
{
File.Create(filePath);
return false;
}
return true;
}等关闭应用程序时需要向刚刚创建的1.txt写入内容:private void FM_Main_FormClosed(object sender, FormClosedEventArgs e)
{
using (StreamWriter sw = new StreamWriter(filePath))
{
sw.Write(this.str_comment);
}
}
当没有 1.txt文件时 关闭程序会抛出: The process cannot access the file because it is being used by another process.的错误。
释放资源不知道该怎么释放, 有没有高手能告诉我how to resolve? 谢谢了!
目录不存在文件名为1.txt
首先WinForm加载,如果不存在文件1.txt那么就创建:private bool InitData(string filePath)
{
if (!File.Exists(filePath))
{
File.Create(filePath);
return false;
}
return true;
}等关闭应用程序时需要向刚刚创建的1.txt写入内容:private void FM_Main_FormClosed(object sender, FormClosedEventArgs e)
{
using (StreamWriter sw = new StreamWriter(filePath))
{
sw.Write(this.str_comment);
}
}
当没有 1.txt文件时 关闭程序会抛出: The process cannot access the file because it is being used by another process.的错误。
释放资源不知道该怎么释放, 有没有高手能告诉我how to resolve? 谢谢了!
解决方案 »
- 求一个backgroundwork的操作事例
- FWTools的OSGeo.GDAL.GdalPINVOKE错!
- C# 操作 Active Directory 报出"登录失败: 未知的用户名或错误密码。"
- C# 图片控件问题pictureBox 错误如何解决
- 请问如何在客户端得到CheckBox选中的节点(TreeView)?
- 哥哥们,马上给分,有没有办法点中DataGridView就是进入了cell?
- 如何使用webservice进行oracle与sqlce同步?
- 水晶報表怎麼根據某字段的值來排名次
- 关于 调用PDF的问题
- 如何将我的程序变成三层结构的???
- 串口读写数据问题
- 如何添加datalist的项 单击 事件??
最简单的办法就是不要调用InitData。(也不必要。即使你程序开始时新建了一个,可能用户删除等原因,程序结束时它也不一定就存在。)
你只要在最后写一次就可以了,设置filemode.openORcreate
FileStream fs = File.Create(filePath);
fs.Close();
直接这样就可以了.
保存一个FileStream 变量然后关闭就OK了.