Я делаю услугу, чтобы поставить на компьютер моего ребенка, чтобы убедиться, что они не могут остановить Qustodio, и они достаточно технически подкованы. Они знают, как остановить службу, перейдя в Run >> services.ms c, и мне нужно сделать службу NOT_STOPPABLE, как у Kaspersky. Но когда я добавляю строку: CanStop = false;
, я не могу запустить службу, и она выдает мне ошибку. Это мой код: </p>
<pre><code>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace MyFirstService
{
public partial class Service1 : ServiceBase
{
Timer timer = new Timer();
ServiceController qengine = new ServiceController("qengine");
protected override void OnStart(string[] args)
{
WriteToFile("Service is started at " + DateTime.Now);
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 5000; //number in milisecinds
timer.Enabled = true;
CanStop = false;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
qengine.Start();
WriteToFile("qengine attemped start at: " + DateTime.Now);
}
public void WriteToFile(string Message)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
if (!File.Exists(filepath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(filepath))
{
sw.WriteLine(Message);
}
}
else
{
using (StreamWriter sw = File.AppendText(filepath))
{
sw.WriteLine(Message);
}
}
}
}
}