参考了这篇文章:
http://blog.csdn.net/sonyicn/archive/2006/05/20/746280.aspx
现实代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace RockLib
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class Utils
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Change user's desktop wallpaper
/// </summary>
/// <param name="picturePath"></param>
/// <param name="style"></param>
/// <returns></returns>
public static bool ChangeWallPaper(string picturePath, WallPaperStyle style)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
RegistryKey myRegKey = Registry.CurrentUser.OpenSubKey("Control Panel\\desktop", true);
switch (style)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case WallPaperStyle.Center:
myRegKey.SetValue("TileWallpaper", "0");
myRegKey.SetValue("WallpaperStyle", "0");
break;
case WallPaperStyle.Tile:
myRegKey.SetValue("TileWallpaper", "1");
myRegKey.SetValue("WallpaperStyle", "0");
break;
case WallPaperStyle.Stretch:
myRegKey.SetValue("TileWallpaper", "0");
myRegKey.SetValue("WallpaperStyle", "2");
break;
default:
throw new NotSupportedException("style");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
myRegKey.Close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int intResult;
if (!File.Exists(picturePath))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentException(string.Format("Picture {0} cannot be found.", picturePath));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
intResult = SystemParametersInfo(20, 3, picturePath, 0x1 | 0x2);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return intResult > 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo
(int uAction, int uParam, string lpvParam, int fuWinIni);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public enum WallPaperStyle
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Center,
Tile,
Stretch
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
测试代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
private void button2_Click(object sender, EventArgs e)
{
string filePath = AppDomain.CurrentDomain.BaseDirectory+ "\\untitled.bmp";
if (!Utils.ChangeWallPaper(filePath, WallPaperStyle.Tile))
{
MessageBox.Show("Change failed.");
}
}