【问题标题】:"The process cannot access the file because it is being used by another process" with Images“该进程无法访问该文件,因为它正被另一个进程使用”与图像
【发布时间】:2012-11-11 20:53:10
【问题描述】:

我见过很多这样的问题已经解决,问题主要是由于流没有被正确处理。

我的问题略有不同,这里按照代码sn-p

 foreach (Images item in ListOfImages)
 {
      newPath = Path.Combine(newPath, item.ImageName + item.ImageExtension);
      File.Create(newPath);

      File.WriteAllBytes(newPath, item.File);
 }

其中Images 是自定义结构,item.File 是原始数据字节[]。

我的问题是在调用WriteAllBytes 的那一行,抛出了一个异常。消息内容如下:

The process cannot access the file because it is being used by another process

再次,我不知道我将如何以某种方式 close 处理。

【问题讨论】:

  • 某些图像是否具有相同的 ImageName 和 ImageExtension?
  • 你确定,路径 newPath 存在吗?
  • 删除File.Create(newPath); 并尝试。 File.WriteAllBytes:Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.msdn.microsoft.com/en-us/library/…
  • 您不处置由 File.Create 创建的流。所以它仍在使用该文件。蒂姆的答案是正确的

标签: c# asp.net file-in-use


【解决方案1】:

由于File.Create 返回流,我会正确处理它:

using(var stream = File.Create(newPath)){}
File.WriteAllBytes(newPath, item.File);

或者您可以使用流直接写入文件:

using (FileStream fs = File.Create(newPath))
{
    fs.Write(item.File, 0, item.File.Length);
}

或者,可能是最简单的,单独使用File.WriteAllBytes

File.WriteAllBytes(newPath, item.File);

创建一个新文件,将指定的字节数组写入文件,然后 然后关闭文件。如果目标文件已经存在,它是 覆盖。

【讨论】:

  • 这...File.WriteAllBytes(newPath, item.File);完美地工作。谢谢
【解决方案2】:

您声明您的问题与处理流无关,但请查看此 MSDN 文章:

http://msdn.microsoft.com/en-us/library/d62kzs03.aspx

File.Create 返回什么?一个文件流!!!!

而且,归根结底,如果File.WriteAllBytes 创建了一个不存在的文件,你为什么还要使用File.Create? ;)

创建一个新文件,将指定的字节数组写入文件,然后 然后关闭文件。如果目标文件已经存在,它是 覆盖。

也可以在 MSDN 上查看:http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx

【讨论】:

    【解决方案3】:
    using (FileStream fs = 
    new FileStream(filePath,
        FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    

    您的日志可能被写锁定,因此请尝试使用 FileShare.ReadWrite。

    【讨论】:

      【解决方案4】:

      create method 打开文件进行写入并返回一个 FileStream 对象供您使用。仅仅因为你没有引用它并不意味着它不需要被返回。

      foreach (Images item in ListOfImages)
                      {
                          newPath = Path.Combine(newPath, item.ImageName + item.ImageExtension);
                          FileStream f = File.Create(newPath);
      
                          f.Write(item.File, 0, item.File.Length);
                      }
      

      【讨论】:

        【解决方案5】:

        File.WriteAllBytes 会在必要时创建文件。你可以直接使用:

        foreach (Images item in ListOfImages)
        {
            newPath = Path.Combine(newPath, item.ImageName + item.ImageExtension);
            File.WriteAllBytes(newPath, item.File);
        }
        

        你是否正确组合路径?

        【讨论】:

          【解决方案6】:

          这是完成您想要做的最具体的方法:

          foreach (Images item in ListOfImages)
          {
              using (System.IO.FileStream output = new System.IO.FileStream(Path.Combine(newPath, item.ImageName + item.ImageExtension),
                  System.IO.FileMode.Create, System.IO.FileAccess.Write))
              {
                  output.Write(item.File, 0, item.File.Length);
                  output.Flush();
                  output.Close();
              }
          }
          

          您还需要修复创建路径的逻辑,我在上面的示例中已完成此操作。您一遍又一遍地连接 newPath。

          【讨论】:

            【解决方案7】:

            强制垃圾收集器清理。

            GC.Collect();
            

            【讨论】:

              猜你喜欢
              • 2010-12-10
              相关资源
              最近更新 更多