Не могу запустить сервис на С#!!! В консоли все работает

Бьюсь два дня! Это сервис который записывает в файл время открытия и закрытия к.л. программы (например пайнт). Если тот же самый код запустить в консоли все работает! помогите пожалуйста.

вот код , код в Program стандартный

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;
using System.Threading.Tasks;

namespace SityMoll
{
public partial class Service1 : ServiceBase
{
    int countGame = 0;
    string path = "C:\\history\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
    bool flagGame = true;
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
            
        var VSMas = Process.GetProcessesByName("mspaint");
        Thread.Sleep(3000);
        if (VSMas.Length > 0)
        {
            var VS = VSMas[0];
            if (VS.VirtualMemorySize64 > 0 && flagGame == true)
            {
                if (countGame == 0)
                {
                    if (File.Exists(path))
                    {
                        StreamReader streamReader = new StreamReader(path);
                        string lastGame = null;
                        while (!streamReader.EndOfStream)
                        {
                            lastGame = streamReader.ReadLine();
                        }
                        int.TryParse(lastGame.Split(new char[] { '-' })[0], out int s);
                        countGame = s;
                        streamReader.Close();
                        streamReader.Dispose();
                    }
                }
                flagGame = false;
                countGame++;

                StreamWriter streamWriter = new StreamWriter(path, true);
                streamWriter.Write(countGame + "- Game started " + DateTime.Now);
                streamWriter.Close();
                streamWriter.Dispose();
            }

        }
        if ((VSMas.Length < 1 || VSMas[0].VirtualMemorySize64 < 0) && flagGame == false)
        {
            flagGame = true;
            StreamWriter streamWriter = new StreamWriter(path, true);
            streamWriter.WriteLine(", closed " + DateTime.Now.ToShortTimeString());
            streamWriter.Close();
            streamWriter.Dispose();
        }

    }

}
}

Ответы (0 шт):