Set desktop wallpaper

leorob88

Well-known member
Joined
Dec 31, 2020
Messages
56
Programming Experience
1-3
I'm willing to fix an application i made. Basically, it sets the desktop wallpaper on windows (i'm on Win10). The only issue is when Windows restarts the wallpaper is gone and it's just black. I'm no expert and I found this code online. I'm wondering if some of you could know how to fix that.

C#:
//inside Form1: Form

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SystemParametersInfo(uint uiAction, uint uiParam, String pvParam, uint fWinIni);

        private const uint SPI_SETDESKWALLPAPER = 0x14;
        private const uint SPIF_UPDATEINIFILE = 0x1;
        private const uint SPIF_SENDWININICHANGE = 0x2;

        private static void DisplayPicture(string file_name)//the string is the file path you want
        {
            uint flags = 0;
            if (!SystemParametersInfo(SPI_SETDESKWALLPAPER,
                    0, file_name, flags))
            {
                Console.WriteLine("Error");
            }
        }
 
Does the file still exist after restarting?
 
Given that you're using SPI_SETDESKWALLPAPER, I'm guessing that there's a corresponding Get value. I'd be inclined to use that after the restart and see if the value has changed. Also, I suspect that you should be using SPIF_UPDATEINIFILE and SPIF_SENDWININICHANGE as the flags when calling SystemParametersInfo. What are they there for otherwise?
 
Given that you're using SPI_SETDESKWALLPAPER, I'm guessing that there's a corresponding Get value. I'd be inclined to use that after the restart and see if the value has changed. Also, I suspect that you should be using SPIF_UPDATEINIFILE and SPIF_SENDWININICHANGE as the flags when calling SystemParametersInfo. What are they there for otherwise?
I don't know at all XD as I said, I found it online and it's out of my knowledge! Also, I don't even know how those variables are actually used. I suppose the first 3 instructions are what does the work but who knows...?
 
Have you made any effort to find out? Have you read the documentation for that function and those constants? We can help but there's still an expectation that you'll do what you can for yourself.
 
Have you made any effort to find out? Have you read the documentation for that function and those constants? We can help but there's still an expectation that you'll do what you can for yourself.
Usually documentation doesn't help so much...still, sometimes the links provided by Skydiver here are very useful!
Don't worry though, I just asked, maybe some of you already worked with this and had a proper answer right away, just that...
 

Latest posts

Back
Top Bottom