zoukankan      html  css  js  c++  java
  • C#创建Windows服务

    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;
     
    namespace WindowsService1
    {
        public partial class Service1 : ServiceBase
        {
            public Service1()
            {
                InitializeComponent();
            }
     
            protected override void OnStart(string[] args)
            {
                try
                {
                    EventLog.WriteEntry("我的服务启动");
                    WriteLog("服务启动");
                    Timer t = new Timer();
                    t.Interval = 1000;
                    t.Elapsed += new ElapsedEventHandler(ChkSvr);
                    t.AutoReset = true;
                    t.Enabled = true;
     
                }
     
                catch (System.Exception ex)
                {
     
                    //错误处理  
     
                }
            }
            public void ChkSvr(object source, ElapsedEventArgs e)
            {
                try
                {
                    Timer tt = (Timer)source;
                    tt.Enabled = false;
                    SendMessahe();
                    tt.Enabled = true;
                }
                catch (Exception ex)
                {
     
                    WriteLog(ex.Message);
                }
            }
            public void SendMessahe()
            {
                try
                {
                    WriteLog("这里是要执行的任务");
                }
                catch (Exception ex)
                {
                    WriteLog(ex.Message);
                }
            }
            public void WriteLog(string read)
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter(@"c:/" + "ceshi.text", true);
                sw.Write("
    事件:" + read + "
    操作时间:" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "");
                sw.Close();
            }
            protected override void OnStop()
            {
                WriteLog("服务停止");
                EventLog.WriteEntry("我的服务停止");
            }
        }
    }
    在安装程序中选中【serviceProcessInstaller1】,查看其属性,将【Account】值改为【LocalSystem】。
    在安装程序中选中【serviceInstaller1】,查看其属性,将【ServiceName】值改为你想要的服务名称。 在目录【C:WindowsMicrosoft.NETFramework】中找到程序对应的.net版本 找到【InstallUtil.exe】 复制到 项目 bin/debug 下

    Install.text里面放如下代码:

    %SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe D:C#Fanso2o_MonitoringActivityFanso2o_MonitoringActivityinDebugFanso2o_MonitoringActivity.exe
    Net Start Fanso2o_MonitoringActivity
    sc config Fanso2o_MonitoringActivity start= auto
    pause

    Uninstall.text里面放如下代码:

    %SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe /u D:C#Fanso2o_MonitoringActivityFanso2o_MonitoringActivityinDebugFanso2o_MonitoringActivity.exe
    pause


    无法打开计算机“.”上的服务控制管理器。此操作可能需要其他特权。:使用管理员权限打开cmd

    再三须慎意,第一莫欺心
  • 相关阅读:
    字符缓冲流,properties类,序列化流与反序列化流,打印流
    字节输入输出流,字符输入输出流,转换流,字节缓冲流
    ListFiles(),文件过滤器与递归
    File
    Beta冲刺第二周王者荣耀交流协会第三次会议
    第九周PSP
    Beta周王者荣耀交流协会第六次会议
    第八周PSP
    王者荣耀交流协会-小组互评Alpha版本
    小组互评Alpha版本
  • 原文地址:https://www.cnblogs.com/otsf/p/8582354.html
Copyright © 2011-2022 走看看