作一个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.   

    File.Create将返回一个FileStream,你没有关闭它将导致后面的问题。
    最简单的办法就是不要调用InitData。(也不必要。即使你程序开始时新建了一个,可能用户删除等原因,程序结束时它也不一定就存在。)
      

  2.   

    你用事先判断时候存在的啊
    你只要在最后写一次就可以了,设置filemode.openORcreate
      

  3.   

    你给我了一个提醒: 
    FileStream fs = File.Create(filePath);
    fs.Close();
    直接这样就可以了.
    保存一个FileStream 变量然后关闭就OK了.