zoukankan      html  css  js  c++  java
  • 文件 正由另一进程使用,因此该进程无法访问此文件

    “文件 正由另一进程使用,因此该进程无法访问此文件”

    看到此问题要明确一点:有两个stream对象对同一个文件发生了操作。如下:

                    using (fs = new FileStream(targetfilepath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        if (File.Exists(targetfilepath)) File.Delete(targetfilepath);
                        
                        fs.Write(exceldata, 0, exceldata.Length);
                    }

    上面的代码有问题吗?有问题!

    在using块中fs只有在运行到最后一个花括号时在关闭。所以在没有关闭前,又使用File.Exists对同一个文件进行操作,所以仍然会报上面的错误。修改方案有两个:把if语句移除using块,或者在执行if语句之前关闭fs(fs.Close())

  • 相关阅读:
    NOIP模拟题 管道
    NOIP模拟题 序列
    NOIP模拟题 栅栏
    NOIP模拟题 斐波那契数列
    CodeForces 797F Mice and Holes
    CodeForces 589H Tourist Guide
    CERC2016 爵士之旅 Jazz Journey
    BZOJ3832 Rally
    BZOJ1061 NOI2008 志愿者招募
    js数组的操作
  • 原文地址:https://www.cnblogs.com/jjhe369/p/2195674.html
Copyright © 2011-2022 走看看