Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have been trying to change the desktop background for a while but i can't do it. is there a way that i can change the desktop background using C#. i have try some code on google and there just make my desktop go black and don't add the image i want.

Please remember that i am using a console appcation (i thing i spelled that right)

What I have tried:

static void Main(string[] args)
{
    string photo = "FUN.PNG";
    SetWallpaper(photo);
}
[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;

public static void SetWallpaper(String path)
{
    SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path,
        SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}
Posted
Updated 15-Jul-18 14:00pm
v2

1 solution

I found out how to do it. ps. you have to use the full path, that is why it didn't work.

C#
        static void Main(string[] args)
        {
            string photo = @"C:\Users\Wolf\source\repos\wolf\wolf\bin\Debug\FUN.PNG";
            DisplayPicture(photo);
        }
        [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)
        {
            uint flags = 0;
            if (!SystemParametersInfo(SPI_SETDESKWALLPAPER,
                    0, file_name, flags))
            {
                Console.WriteLine("Error");
            }
        }
    }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900