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

喜你入骨 提交于 2019-11-27 16:04:25

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.

You state that your problem has nothing to do with disposing streams but check this MSDN article:

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

What does File.Create return? A FileStream!!!!

And, at the end of the day, why are you using File.Create if File.WriteAllBytes creates a file if this doesn't exist? ;)

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.

Check it on MSDN too: http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx

using (FileStream fs = 
new FileStream(filePath,
    FileMode.Open, FileAccess.Read, FileShare.ReadWrite))

Your log may be write locked, so try with FileShare.ReadWrite.

just.another.programmer

The create method opens the file for writing and returns a FileStream object for you to work with. Just because you are not referencing it does not mean it does not need to be returned.

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);
                }

The File.WriteAllBytes creates the file if necessary. You can juts use:

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

And are you combine path correctly?

This is the most specific way to accomplish what you are trying to do:

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();
    }
}

You also need to fix your logic for creating the path, which I have done in my example above. You were concatenating the newPath over and over again.

NoloMokgosi

Force the garbage collector to clean.

GC.Collect();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!