C# Setting Wallpaper from Current working Directory, getting Error: System.IO.FileNotFoundException: 'C:\Saitama.png'

Viewed 26

Hello so i ran in a Problem that i could't fix since some days and i thought you people could help me. I want to set the Wallpaper Image from my working directory. It's like this Directory how i pasted the Image that should be the Wallpaper

and my Method to set it as a Wallpaper is this

public sealed class Wallpaper
{
    const int SPI_SETDESKWALLPAPER = 20;
    const int SPIF_UPDATEINIFILE = 0x01;
    const int SPIF_SENDWININICHANGE = 0x02;

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

    public enum Style : int
    {
        Tiled,
        Centered,
        Stretched
    }

    public static void Set(string imgPath)
    {
        string exeDir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        Directory.SetCurrentDirectory(exeDir);

        var img = System.Drawing.Image.FromFile(imgPath);
        string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "wallpaper.bmp");
        img.Save(tempPath, ImageFormat.Bmp);

        var key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);

        key.SetValue(@"WallpaperStyle", 1.ToString());
        key.SetValue(@"TileWallpaper", 0.ToString());


        SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, tempPath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
    }
}

And i want to set the Image Wallpaper like this

Wallpaper.Set(@"/Saitama.png");

But everytime i get this Error

System.IO.FileNotFoundException: 'C:\Saitama.png'

That C:\Saitama.png works when i want to set that Image as the Source on XAML but not on this code i have to give a full Path why? And how could I make it that i can give the path like above?

Please help thanks :D

1 Answers

Well i didn't found a good Answer but this still makes it's job. I just made Setup Wizard and that Wizard just downloades the Images in the selected Folder and then the Program just uses the Full Path of the Destination where the Image is. I hope i could help someone!

Related