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

    再三须慎意,第一莫欺心
  • 相关阅读:
    sqlserver中判断表或临时表是否存在
    Delphi 简单方法搜索定位TreeView项
    hdu 2010 水仙花数
    hdu 1061 Rightmost Digit
    hdu 2041 超级楼梯
    hdu 2012 素数判定
    hdu 1425 sort
    hdu 1071 The area
    hdu 1005 Number Sequence
    hdu 1021 Fibonacci Again
  • 原文地址:https://www.cnblogs.com/otsf/p/8582354.html
Copyright © 2011-2022 走看看