我希望使用C#服务为Windows 7设置壁纸。 当服务作为控制台应用程序运行时,这可以正常工作。但 安装服务并启动它后,它不会切换 壁纸。任何人都知道如何在窗口内设置壁纸 服务?
这是我的代码:
private String file = @"C://Users//Alvin//Pictures//onepiece.jpg";
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
SetWallpaper(file, 0);
}
private void SetWallpaper(string WallpaperLocation, int WallpaperStyle)
{
try
{
// Sets the actual wallpaper
SystemParametersInfo(20, 0, "@" + WallpaperLocation, 0x01 | 0x02);
// Set the wallpaper style to streched (can be changed to tile, center, maintain aspect ratio, etc.
RegistryKey rkWallPaper = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
// Sets the wallpaper style
switch (walpaperStyle)
{
case 0:
rkWallPaper.SetValue(@"WallpaperStyle", "0");
rkWallPaper.SetValue(@"TileWallpaper", "1");
break;
case 1:
rkWallPaper.SetValue(@"WallpaperStyle", "0");
rkWallPaper.SetValue(@"TileWallpaper", "0");
break;
case 2:
rkWallPaper.SetValue(@"WallpaperStyle", "2");
rkWallPaper.SetValue(@"TileWallpaper", "0");
break;
case 3: // (Windows 7 and later)
rkWallPaper.SetValue(@"WallpaperStyle", "6");
rkWallPaper.SetValue(@"TileWallpaper", "0");
break;
case 4: // (Windows 7 and later)
rkWallPaper.SetValue(@"WallpaperStyle", "10");
rkWallPaper.SetValue(@"TileWallpaper", "0");
break;
}
rkWallPaper.Close();
cetakService("sukses set walpaper");
}
catch (Exception e)
{
cetakService("Error "+e.Message.ToString());
}
}
答案 0 :(得分:1)
如果您希望不时切换壁纸,请注意OnStart()在服务启动时运行一次。我不知道你是如何通过SetWallpaper改变壁纸的。它实际上运行一次并设置壁纸。没有实现逻辑来继续更改要执行的不同情况的壁纸。 此代码应在服务首次启动时更改壁纸,如果您希望这样做,请确保您的服务具有访问注册表值的足够权限。