How to Get/Set Windows 7 Wallpaper name in C#

Hello experts,

I'm trying to set and get the current Wallpaper of windows 7, the problem is in Regisrty the Wallpaper key is always the same and doesnt change:

AppData\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper.jpg

However, if i navigate to the folder, i found the new image saved with
TranscodedWallpaper.jpg.

For the set, if i used SystemParametersInfo, the registry key is changed to the image i specify, but the Whole Visual Styles in windows doesnt function -you select any theme or any wallpaper in the personalize dialog, nothing changes-.

So in short, i need a way to get the current wallpaper name and set the wallpaper to any image without affecting windows functionality in Themes and visual styles.

Thanks.
YazArchAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Death259Commented:
Here's some sample code from: http://social.msdn.microsoft.com/Forums/en/windowssdk/thread/8a07f1f9-30ba-40ea-9eef-5f405d666849
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32 uiParam, String pvParam, UInt32 fWinIni);
private static UInt32 SPI_SETDESKWALLPAPER = 20;
private static UInt32 SPIF_UPDATEINIFILE = 0x1;
private String imageFileName = "c:\\sample.bmp";

public void SetImage( string filename )
{
    SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, filename, SPIF_UPDATEINIFILE);
}

Open in new window

0
YazArchAuthor Commented:
Thanks for your replies, i've already tried the described methods and they dont work on windows 7 as following:

1. The registry key always return TranscodedWallpaper.jpg.
2. SystemParametersInfo works in changing the wallpaper and setting the registry key to the image file BUT it screws Windows personalization dialog as nothing will be functional there.

P.S: i've also tried to pass SPIF_UPDATEINIFILE and SPIF_SENDWININICHANGED.

I need solution for windows 7, the proposed solutions work on previous versions of windows but in win7 they cause problems.

Is there a way to set it/get it through uxTheme.dll??
0
CuteBugCommented:
Hi,
This code will set the wallpaper
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    [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)
    {
        if (File.Exists(path))
        {
            Image imgInFile = Image.FromFile(path);
            try
            {
                SystemParametersInfo(SPI_SETDESKWALLPAPER, 3, path, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
            }
            catch
            {
                MessageBox.Show("error in setting the wallpaper");
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.CheckFileExists = true;
        ofd.CheckPathExists = true;
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            SetWallpaper(ofd.FileName);
        }
    }

}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YazArchAuthor Commented:
Thanks, appearantly the problem was in:

SPI_SETDESKWALLPAPER = 20;

it should be SPI_SETDESKWALLPAPER = 0x14

And uParam was 0 and it should be 3.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.