110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;
namespace Service_Windows
{
public partial class Service_Windows : ServiceBase
{
public Service_Windows()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
huanbizhi hbz = new huanbizhi();
hbz.huan(@"H:\3号机备分\bak\照片", 5, 0);
}
protected override void OnStop()
{
}
}
public class huanbizhi
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
[DllImport("user32.dll")]
public static extern bool SetSysColors(int cElements, int[] lpaElements, int[] lpaRgbValues);
public void huan(string path, int time, int yangshi)
{
string[] filename = Directory.GetFiles(path);
Random rd = new Random();
bool b = true;
while (b)
{
ChangeWallPaper(filename[rd.Next(0, filename.Length - 1)], yangshi);
System.Threading.Thread.Sleep(time * 1000);
}
}
public void ChangeWallPaper(string picturePath, int style)
{
try
{
//修改壁纸注册表
RegistryKey myRegKey = Registry.CurrentUser.OpenSubKey("Control Panel\\desktop", true);
switch (style)
{
case 0: //居中
myRegKey.SetValue("TileWallpaper", "0");
myRegKey.SetValue("WallpaperStyle", "0");
break;
case 1: //平铺
myRegKey.SetValue("TileWallpaper", "1");
myRegKey.SetValue("WallpaperStyle", "0");
break;
case 2: //拉伸
myRegKey.SetValue("TileWallpaper", "0");
myRegKey.SetValue("WallpaperStyle", "2");
break;
}
myRegKey.SetValue("WallPaper", picturePath);
myRegKey.Close();
//修改背景色注册表
RegistryKey bKey = Registry.CurrentUser.OpenSubKey("Control Panel\\Colors", true);
bKey.SetValue("Background", 0 + " " + 0 + " " + 255);
int[] elements = { 1 };
int[] colors = { 0, 0, 255 };
SetSysColors(elements.Length, elements, colors);
bKey.Close();
//更新图片
if (File.Exists(picturePath))
{
//nResult = SystemParametersInfo(20, 1, @"F:\图片\新建文件夹\1.jpg", 0x1 | 0x2);
//nResult = SystemParametersInfo(20, 3, @"F:\exin.JPG", 0x1 | 0x2);
if (SystemParametersInfo(20, 1, picturePath, 1) == 0)
{
throw new Exception("更换壁纸失败");
}
}
}
catch
{
throw new Exception("更换壁纸失败");
}
}
}
}
using System;
using System.ServiceProcess;
namespace Service_Windows
{
internal static class Program
{
private static void Main()
{
ServiceBase.Run(new ServiceBase[] { new Service_Windows () });
}
}
}