Question

Je me suis limité à cette méthode, mais je ne comprends pas pourquoi il verrouille le fichier. Je crois que vous pourriez utiliser quelque chose comme

using( something)
{

//do stuff here
}

Mais je ne suis pas sûr que cela A) résoudrait le problème ou B) serait la bonne façon de le faire.

des idées?

[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");
        }
    }
#

Code mis à jour

 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();
            }
        }
    }
Était-ce utile?

La solution

De MSDN : " Le fichier reste verrouillé jusqu'à la L'image est supprimée. " - donc oui, cela devrait être corrigé par:

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

(Remarque: je resserrerais la capture d'essai aux seuls appels .Save () et / ou SystemParametersInfo ())

Autres conseils

Une fois que vous avez quitté le bloc à l'aide de , tous les objets initialisés dans ce bloc sont supprimés. Dans votre cas, les objets seront supprimés, ce qui supprimera le verrou sur le fichier.

Vous devez éliminer manuellement (via une instruction à l'aide de ou en appelant .Dispose () ) tout appel non géré à des fonctions d'API COM ou Windows (c'est-à-dire lorsque vous utilisez interop).

Voici ce que j'ai s'il vous plaît faites le moi savoir si vous voyez quelque chose que je pourrais assez haut. Mais cela fonctionne comme un champion, donc je suis assez heureux.

                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");
                    }
                }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top