.net window service

오민석·2021년 9월 22일
0
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.Tasks;
using System.Timers;
using System.IO;
using System.ComponentModel;
using System.Configuration.Install;

namespace today
{
    public partial class Service1 : ServiceBase
    {
        private Timer timer;
        private const string folderpath = "C://";
        private const string filePath = folderpath + "test.txt";
        StreamWriter sw;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            if (!Directory.Exists(folderpath))
                Directory.CreateDirectory(folderpath);
            writelog("start");

            timer = new Timer();
            timer.Interval = 1000;
            timer.Elapsed += Timer_Elapsed;
            timer.Start();
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            sw = new StreamWriter(filePath, true);
            sw.WriteLine(DateTime.Now.ToString());
            sw.Close();
        }

        protected override void OnStop()
        {
            writelog("stop");
        }
        private void writelog(string strMsg)
        {
            sw = new StreamWriter(filePath, true);
            sw.WriteLine(strMsg);
            sw.Close();
        }
    }
}
  1. 왼쪽 탭의 Service.cs누르고 오른쪽 마우스 눌러서 설치관리자

  2. 왼쪽 탭의 ProjectInstaller.cs 누르고 F4 눌러서 Account: LocalSystem

  3. vs cmd의 관리자 실행 모드로 installutil.exe today.exe

0개의 댓글

관련 채용 정보