cannot save file because it's in use

353 views Asked by At

I've tried so many different ways to do this, and it's still telling me the file is in use when I try to delete it before saving it again.

if (Clipboard.GetDataObject().GetData(DataFormats.Bitmap) != null)
{
    if (File.Exists(filename)) { File.Delete(filename); }
    using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
    {
        ImageConverter converter = new ImageConverter();
        byte[] bytes = (byte[])converter.ConvertTo(Clipboard.GetDataObject().GetData(DataFormats.Bitmap), typeof(byte[]));
        fs.Write(bytes, 0, bytes.Length);
        fs.Close();
        fs.Dispose();
    }
}

As you can see, I have everything in a "using" block. I also "close" the file, and even tried explicitly calling the "dispose" as well. But when I run the code again, it STILL tells me the file is in use. What can I do to be able to overwrite the file?

I've also tried it this way:

using (Bitmap bmp = new Bitmap((Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap)))
{
    if (bmp != null)
    {
        if (File.Exists(filename)) { File.Delete(filename); }
        using (MemoryStream memory = new MemoryStream())
        {
            using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
            {
                bmp.Save(memory, ImageFormat.Bmp);
                byte[] bytes = memory.ToArray();
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();
            }
            memory.Dispose();
        }
        bmp.Dispose();
        break;
    }
}

and it still gives me the same error.

1

There are 1 answers

2
Curtis On

If the file is already locked before your program runs, you won't be able to open it. See if you can unlock it using a program like Unlocker. Once the file is unlocked, you should be able to open it again.

When you wrap the file streams into a using{} statement, the close(); and dispose(); calls are not necessary. They are done for you as the code exits the using{} statement. It doesn't hurt to call close(); but it is redundant. If you call dispose(); however, and the end of the using calls dispose(); too, you will likely get an exception because you're calling dispose(); on a disposed object. Remove your calls to close(); and dispose();

If you 'end process' on your program via Task Manager (mangler) or click the 'stop' button inside your IDE while the program was looping inside the using{} block, it will lock the file again and you'll need to use 'Unlocker' again.

So, remove your close() and dispose() calls and then use 'Unlocker' to unlock the file. Run your program, and you should be golden...

UPDATE:

Put a try/catch block around the following code:

        bmp.Save(memory, ImageFormat.Bmp);
        byte[] bytes = memory.ToArray();
        fs.Write(bytes, 0, bytes.Length);

See if you ever get an exception in the above chunk of code. If you put a try/catch around it, it might point to to why the problem is happening..