Iveはこの方法に絞られましたが、なぜファイルをロックするのか分かりません。私はあなたがそれがなかった場合は、正しい方法であること)このコードはなぜ私のファイルをロックするのですか?
using(something)
{
//do stuff here
}
しかし、イムそれはA)問題またはBを解決するだろうかわからないようなものを使用することができます信じています。
[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");
}
}
#
更新されたコードは
MSDNから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();
}
}
}
私はまたに投げるだろう:(File.exists(パス))場合は{..} – Nippysaurus
@Nippysaurus:私はSetWallpaper()、自分自身をスローするようにしたいと思います。 –
私の質問は、メソッドの実行が終了したときとどう違うのかと思います。完了後にイメージが自動的に廃棄されるべきではありませんか? – Crash893