相关疑难解决方法(0)

系统IO异常:进程无法访问该文件,因为它正由另一个进程c#使用

我已经看到了这个问题的几个帖子.我已经实现了所有建议,比如在streamwriter和连接对象上使用flush(),close()方法,使用GC.Collect()强制清理,使用{}来自动执行

我正在从DB做简单的操作并写入文本文件..这是我的代码

public void WriteToFile(string ProductName)
{
    //Already Got Data from DB and stored in "ProductName" 

   //saving in File
    if (!File.Exists(path11))
    {
        File.Create(path11);
        StreamWriter tw = new StreamWriter(path11);
        tw.WriteLine(ProductName+"@"+DateTime.Now.ToString());

        tw.Flush();
        tw.Close();
    }
    else if (File.Exists(path11))
    {
        StreamWriter tw = new StreamWriter(path11, true);
        tw.WriteLine(ProductName + "@" + DateTime.Now.ToString());

        tw.Flush();
        tw.Close();
    }

    GC.Collect();
}
Run Code Online (Sandbox Code Playgroud)

我得到的另一个建议是锁定对象..但我无法实现它..任何建议都会有帮助

.net c#

2
推荐指数
1
解决办法
829
查看次数

标签 统计

.net ×1

c# ×1