문제

나는이 방법으로 좁혀졌지만 왜 파일을 잠그는 지 이해하지 못한다. 나는 당신이 같은 것을 사용할 수 있다고 생각합니다

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

업데이트 된 코드

 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();
            }
        }
    }
도움이 되었습니까?

해결책

에서 MSDN: "이미지가 배치 될 때까지 파일이 잠겨 있습니다." - 예, 이것은 다음과 같이 해결해야합니다.

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

(부수적으로, 나는 try catch를 .save () 및/또는 systemParametersinfo () 호출에만 조입니다.

다른 팁

일단 당신이 나가면 사용 블록, 그 안에 초기화 된 모든 객체가 배치됩니다. 귀하의 경우, 객체가 배치되어 파일의 잠금이 제거됩니다.

수동으로 처분해야합니다 (a 사용 진술 또는 전화로 .Dispose () 객체에서) COM 또는 Windows API 함수에 대한 관리되지 않는 호출 (즉, Interop을 사용할 때).

여기에 내가 가지고있는 것이 있습니다. 내가 꽤 꽤 볼 수있는 것을 보면 알려주세요. 그러나 그것은 챔피언처럼 일하기 때문에 나는 매우 행복합니다.

                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");
                    }
                }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top