why does this code lock my files?
Asked Answered
D

3

5

Ive narrowed down to this method but i don't understand why its locking the file. I believe you could use something like

using( something)
{

//do stuff here
}

But im not sure that would A) solve the issue or B) be the correct way if it did.

any ideas?

[DllImport("user32.dll", CharSet = CharSet.Auto)]private static extern Int32 SystemParametersInfo(UInt32 action, UInt32 uParam, String vParam, UInt32 winIni);  
    private static readonly UInt32 SPI_SETDESKWALLPAPER  = 0x14;  
    private static readonly UInt32 SPIF_UPDATEINIFILE    = 0x01;  
    private static readonly UInt32 SPIF_SENDWININICHANGE = 0x02;  

    private void SetWallpaper(string path)
    {
        try
        {
            Image imgInFile = Image.FromFile(path);
            imgInFile.Save(SaveFile, ImageFormat.Bmp);
            SystemParametersInfo(SPI_SETDESKWALLPAPER, 3, SaveFile, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
        }
        catch
        {
            MessageBox.Show("error in setting the wallpaper");
        }
    }
#

Updated code

 private void SetWallpaper(string path)
    {
        if (File.Exists(path))
        {
            Image imgInFile = Image.FromFile(path);
            try
            {
                imgInFile.Save(SaveFile, ImageFormat.Bmp);
                SystemParametersInfo(SPI_SETDESKWALLPAPER, 3, SaveFile, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
            }
            catch
            {
                MessageBox.Show("error in setting the wallpaper");
            }
            finally
            {
                imgInFile.Dispose();
            }
        }
    }
Domineer answered 30/4, 2009 at 1:39 Comment(0)
C
15

From MSDN: "The file remains locked until the Image is disposed." - so yes, this should be fixed by:

using (Image imgInFile ...) { ... }

(As a side note, I would tighten the try catch to just the .Save() and/or SystemParametersInfo() calls)

Churchwarden answered 30/4, 2009 at 1:39 Comment(4)
I'd also throw in : if(File.exists(path)) { .. }Squid
@Nippysaurus: I would like SetWallpaper() to throw, myself.Churchwarden
i guess my question is how is that different than when the method is finished running. shouldn't the image be automatically disposed of after it completes?Domineer
No. It is automaticly disposed when it is garbage collected, but that could be anything from a few seconds to never from then (excluding forced collecting, which you should pretty much never do).Churchwarden
E
1

Once you pass out of the using block, all objects initialized within it are disposed. In your case, the objects will be disposed which will remove the lock on the file.

You must manually dispose (either through a using statement or by calling .Dispose() on the object) any unmanaged calls to either COM or Windows API functions (i.e. when you use interop).

Estrange answered 30/4, 2009 at 1:39 Comment(1)
using only calls .Dispose() on the expression inside the parens.Churchwarden
D
0

Here is what i have please let me know if you see anything that i could pretty up. But its working like a champ so i'm pretty happy.

                private void SetWallpaper(string path)
                {
                    if (File.Exists(path))
                    {
                        Image imgInFile = Image.FromFile(path);
                        try
                        {
                            imgInFile.Save(SaveFile, ImageFormat.Bmp);
                            SystemParametersInfo(SPI_SETDESKWALLPAPER, 3, SaveFile, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
                        }
                        catch
                        {
                            MessageBox.Show("error in setting the wallpaper");
                        }
                        finally
                        {
                            imgInFile.Dispose();
                        }
                    }

                    Else
                    {
                          messagebox.show("Error with path: "+path+" Not found or in use");
                    }
                }
Domineer answered 30/4, 2009 at 1:39 Comment(1)
This should behave the same as if (File.Exists(path)) using(Image imgInFile = Image.FromFile(path)) try {...} catch {...} (no finally). But thats just style.Churchwarden

© 2022 - 2024 — McMap. All rights reserved.