Служба скриншот рабочего стола - C#
Формулировка задачи:
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.Threading;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace Service
{
public partial class Service : ServiceBase
{
private StreamWriter file;
//Определяем таймер
private System.Timers.Timer timer1;
public Service()
{
InitializeComponent();
}
static string GenerateRandomJpegName()
{
return GenerateRandomString() + ".jpg";
}
static string GenerateRandomString()
{
bool UseSigns = true;
bool UseUpperLetters = true;
bool UseLetters = true;
int Length;
NewLabel:
try
{
Length = new Random(DateTime.Now.Millisecond - DateTime.Now.Second + new Random(DateTime.Now.Millisecond).Next(0, 100) / new Random(DateTime.Now.Millisecond - DateTime.Now.Second).Next(0, 10)).Next(0, 100);
}
catch { goto NewLabel; }
string result = "C:/";
try
{
int Seed = 0;
char[] letters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
char[] signs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
List<char> keyWords = new List<char>();
List<char> upperLetters = new List<char>();
foreach (char c in letters)
upperLetters.Add(Convert.ToChar(c.ToString().ToUpper()));
if (UseLetters)
foreach (char c in letters)
keyWords.Add(c);
if (UseSigns)
foreach (char c in signs)
keyWords.Add(c);
if (UseUpperLetters)
foreach (char c in upperLetters)
keyWords.Add(c);
int MaxValue = keyWords.Count;
for (int i = 0; i <= Length; i++)
{
try
{
Random mainrand = new Random(Seed);
char RandChar = keyWords[mainrand.Next(0, MaxValue)];
result += RandChar;
Seed += DateTime.Now.Millisecond + Seed - new Random().Next(10) + new Random(DateTime.Now.Millisecond + 800 * 989 / 3).Next(10);
}
catch { continue; }
}
}
catch { }
return result;
}
static void Main()
{
}
protected override void OnStart(string[] args)
{
//Файл по умолчанию будет создан в "C:\Windows\SysWOW6"
file = new StreamWriter(new FileStream("Service.log", System.IO.FileMode.Append));
this.file.WriteLine("Service стартовал");
this.file.Flush();
//Создаем таймер и выставляем его параметры
this.timer1 = new System.Timers.Timer();
this.timer1.Enabled = true;
//Интервал 10000мс - 10с.
this.timer1.Interval = 100000;
this.timer1.Elapsed +=
new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
this.timer1.AutoReset = true;
this.timer1.Start();
Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
bmp.Save(GenerateRandomJpegName(), ImageFormat.Jpeg);
}
protected override void OnStop()
{
this.timer1.Stop();
this.file.WriteLine("Service остановлен");
this.file.Flush();
this.file.Close();
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//Записываем время в файл или делаем все, что хотим
this.file.WriteLine(DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
this.file.Flush();
}
}
}Решение задачи: «Служба скриншот рабочего стола»
textual
Листинг программы
private void serviceInstaller1_Committed(object sender, InstallEventArgs e)
{
string serviceName = "MyRss";
ManagementObjectSearcher ms =
new ManagementObjectSearcher("SELECT * FROM Win32_Service WHERE DisplayName = '" + serviceName + "'");
foreach (ManagementObject mo in ms.Get())
{
ManagementObject inParams = (ManagementObject)mo.GetMethodParameters("Change");
inParams["DesktopInteract"] = true;
mo.InvokeMethod("Change", inParams, null);
break;
}
}