• 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

    再三须慎意,第一莫欺心
  • 相关阅读:
    运行自动安装apk代码,报错: Original error: Could not find aapt Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
    已安装Appium-Python-Client,但appium无法导入WebDriver
    打开uiautomatorviewer.bat闪退
    常见的python面试题【杭州多测师】【python笔试题】
    支付功能怎么测试?
    自动化运维工具——ansible详解
    服务端编程——异常+校验器+环境变量
    服务端编程——初始koa
    用postman发送请求,在koa中获取的请求body为undefined
    jQuery入口函数测试
  • 原文地址:https://www.cnblogs.com/otsf/p/8582354.html
Copyright © 2020-2023  润新知