• 创建windows 服务 C#


    Daemon内容如下:

     public partial class Daemon : ServiceBase
            {
                private readonly Timer _timer;
                private readonly Worker _worker;
                private readonly EventLog _eventLog;
                public Daemon()
                {
                    InitializeComponent();
                    _timer = new Timer(5000);
                    _worker = new Worker();
                }
    
                protected override void OnStart(string[] args)
                {
                    _timer.Start();
                    _timer.Elapsed += _worker.Test;//Test
    
                }
    
                protected override void OnStop()
                {
                    _timer.Stop();
                }
            }
    View Code

    Worker 类

     public class Worker
        {
            public void Test(object sender, ElapsedEventArgs e)
            {
                WriteLog(DateTime.Now.ToString());
            }
            public void WriteLog(string error)
            {
                FileStream fs = new FileStream(@"d:wangzihao.txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.BaseStream.Seek(0, SeekOrigin.End);
                sw.WriteLine(error + DateTime.Now.ToString() + "
    ");
    
                sw.Flush();
                sw.Close();
                fs.Close();
            }
    
    
            private void writeLog(Exception e)
            {
                FileStream fs = new FileStream("d:/wangzihao.txt", FileMode.OpenOrCreate);
                //获得字节数组
                byte[] data = new UTF8Encoding().GetBytes(e.Message);
                //开始写入
                fs.Seek(0, SeekOrigin.End);
                fs.Write(data, 0, data.Length);
                var str = "
    ";
                byte[] data1 = new UTF8Encoding().GetBytes(str);
                fs.Write(data1, 0, data1.Length);
                //清空缓冲区、关闭流
                fs.Flush();
                fs.Close();
            }
        }
    View Code

    下面重点来了:

    为服务创建安装程序

      1.. 返回到 Service1 的“设计”视图。
      2.. 单击设计器的背景以选择服务本身,而不是它的任何内容。
      3.. 在“属性”窗口中,单击属性列表下面灰色区域中的“添加安装程序”链接。
      默认情况下,向您的项目添加包含两个安装程序的组件类。将该组件命名为
    ProjectInstaller,它包含的安装程序分别是服务的安装程序和服务关联进程的安装程
    序。

      4.. 访问 ProjectInstaller 的“设计”视图,然后单击“ServiceInstaller1”。
      5.. 在“属性”窗口中,将 ServiceName 属性设置为 MyNewService。//f
      6.. 将 StartType 属性设置为 Automatic。
      7.. 在设计器中,选择 ServiceProcessInstaller1(针对 Visual Basic 项目),
    或 serviceProcessInstaller1(针对 Visual C# 项目)。将 Account 属性设置为
    LocalService。这将使得在本地服务帐户上安装和运行该服务。有关更多信息,请参见
    ServiceProcessInstaller.Account 属性。
        安全说明   LocalService 帐户用作本地计算机上的非特权用户,向任何远程服务
    器显示匿名凭据。使用其他帐户时需要特别小心,因此它们具有较高的特权,会增加您
    受到恶意代码攻击的风险。

  • 相关阅读:
    中国正在消失的老行当
    ie9 scrollbar中hover 高度增高的bug
    (替月光博客备份)百度百科:游荡在中国的窃贼
    严格模式下 W3C Strict 验证的几个注意事项
    [转]滤镜渐变使用 IE浏览器
    1.什么是串口?
    6.串口操作之API篇 GetCommTimeouts SetCommTimeouts
    5.串口操作之API篇 SetupComm GetCommState SetCommState
    TeeChart经验总结 13.Export之2.对象保存
    解决"手机存储暂不能使用""SIM卡存储暂不能使用"
  • 原文地址:https://www.cnblogs.com/WZH75171992/p/6610256.html
Copyright © 2020-2023  润新知