CanStop = false,但我无法启动服务

问题描述 投票:0回答:1

我正在提供一项服务以安装在我孩子的计算机上,以确保他们无法阻止Qustodio,而且他们精通技术。他们知道如何通过运行>>服务.msc来停止服务,并且我需要使服务成为NOT_STOPPABLE,例如Kaspersky。但是,当我添加以下行:CanStop = false;时,无法启动该服务,这给了我一个错误。这是我的代码:

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);
                }
            }
        }
    }
}

c# windows-services
1个回答
0
投票

无法设置CanStop = false;服务启动后。

© www.soinside.com 2019 - 2024. All rights reserved.