XAML C#正在从当前工作目录设置墙纸,出现错误:System.IO.FileNotFoundException:'C:\琦玉. png'

omjgkv6w  于 2022-12-07  发布在  C#
关注(0)|答案(1)|浏览(116)

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

aiqt4smr

aiqt4smr1#

好吧,我没有找到一个好的答案,但这仍然使它的工作。我只是做了安装向导,该向导只是下载所选文件夹中的图像,然后程序只是使用图像所在的目的地的完整路径。我希望我能帮助别人!

相关问题