我希望为使用C#服务的Windows 7设置壁纸。 当服务作为控制台应用程序运行时,此工作正常。但在安装服务并启动它之后,它不会在 壁纸之间切换,然后 。任何人有一个想法如何设置窗口 服务内的壁纸?如何更改壁纸使用服务c#
这里是我的代码:
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());
}
}
您是否尝试将用户的服务临时运行到管理员帐户,以确保它是您的代码的问题而不是权限? – JMK
我已将服务更改为本地系统帐户,但不起作用 – user3012452
您使用Windows的实际帐户情况如何? – JMK