Domanda

Ho ristretto a questo metodo ma non capisco perché blocca il file. Credo che potresti usare qualcosa del genere

using( something)
{

//do stuff here
}

Ma non sono sicuro che A) risolva il problema o B) sia il modo corretto se lo facesse.

qualche idea?

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

Codice aggiornato

 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();
            }
        }
    }
È stato utile?

Soluzione

Da MSDN : " Il file rimane bloccato fino al L'immagine è eliminata. & Quot; - quindi sì, questo dovrebbe essere risolto da:

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

(Come nota a margine, vorrei restringere il tentativo di cattura solo alle chiamate .Save () e / o SystemParametersInfo ())

Altri suggerimenti

Una volta usciti dal blocco usando , tutti gli oggetti inizializzati al suo interno vengono eliminati. Nel tuo caso, gli oggetti verranno eliminati e ciò rimuoverà il blocco sul file.

È necessario disporre manualmente (tramite un'istruzione utilizzando o chiamando .Dispose () sull'oggetto) tutte le chiamate non gestite a funzioni API COM o Windows (ad es. quando usi l'interoperabilità).

Ecco quello che ho, per favore fatemi sapere se vedete qualcosa che potrei trovare. Ma funziona come un campione, quindi sono abbastanza felice.

                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");
                    }
                }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top