“The process cannot access the file because it is being used by another process” with Images

前端 未结 7 1046
梦毁少年i
梦毁少年i 2020-12-04 02:02

I\'ve seen many issues like this that have been solved and the problem was mostly due to streams not being disposed of properly.

My issue is slightly different, here

7条回答
  •  广开言路
    2020-12-04 02:05

    Since File.Create returns the stream i would dispose it properly:

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

    or you can use the stream to write to the file directly:

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

    or, probably the easiest, use File.WriteAllBytes alone:

    File.WriteAllBytes(newPath, item.File);
    

    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.

提交回复
热议问题