[转]C#创建服务及使用程序自动安装服务,.NET创建一个即是可执行程序又是Windows服务的exe...

写在前面

原文地址:C#创建服务及使用程序自动安装服务,.NET创建一个即是可执行程序又是Windows服务的exe

这篇文章躺在我的收藏夹中有很长一段时间了,今天闲着没事,就自己动手实践了一下。感觉作者还是蛮给力的。这里动手实践,把它变为自己的东西。多谢作者。

操作步骤

最近在弄的项目,一直在考虑是否弄windows服务来定时拉取数据。然后就按照这个思路,想了一下,发现跟之前看的一篇文章需求很是类似。就弄了个demo,把那种方式吸收下来。

首先,创建一个控制台应用程序。

然后,添加一个windows服务类。

项目

然后打开SericeHost.cs,右键查看代码

代码如下:

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;

namespace Wolfy.SelfService
{
    partial class ServiceHost : ServiceBase
    {
        public ServiceHost()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            System.IO.File.AppendAllText("D:\\log.txt", "Service Is Started……" + DateTime.Now.ToString());
        }

        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            System.IO.File.AppendAllText("D:\\log.txt", "Service Is Stopped……" + DateTime.Now.ToString());
        }
    }
}

修改入口函数Main方法,根据agrs参数,来识别是安装服务,还是应用程序。如果是服务则执行安装服务的步骤,否则则运行程序。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace Wolfy.SelfService
{
    class Program
    {
        static void Main(string[] args)
        {
            //如果传递了"s"参数就启动服务
            if (args.Length > 0 && args[0] == "s")
            {
                //启动服务的代码,可以从其它地方拷贝
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new ServiceHost(),
                };
                ServiceBase.Run(ServicesToRun);
            }
            else
            {
                Console.WriteLine("这是Windows应用程序");
                Console.WriteLine("请选择,[1]安装服务 [2]卸载服务 [3]退出");
                var rs = int.Parse(Console.ReadLine());
                switch (rs)
                {
                    case 1:
                        //取当前可执行文件路径,加上"s"参数,证明是从windows服务启动该程序
                        var path = Process.GetCurrentProcess().MainModule.FileName + " s";
                        Process.Start("sc", "create myserver binpath= \"" + path + "\" displayName= 我的服务 start= auto");
                        Console.WriteLine("安装成功");
                        Console.Read();
                        break;
                    case 2:
                        Process.Start("sc", "delete myserver");
                        Console.WriteLine("卸载成功");
                        Console.Read();
                        break;
                    case 3: break;
                }
            }

        }
    }
}

测试
以管理员身份运行Wolfy.SelfService.exe可执行程序。

这里需要注意,要以管理员身份运行,毕竟是安装windows服务,否则权限不够,造成安装失败。

输入1

然后win+R,输入services.msv

服务安装成功,下面我们启动服务。

查看下log.txt

说明服务安装成功,并启动成功。

停止服务,然后卸载服务。

Service Is Started……2015/5/8 21:14:11Service Is Stopped……2015/5/8 21:16:21

说明服务停止成功,然后进行卸载。

同样,卸载也要以管理员身份运行。

然后在服务管理中,发现“我的服务”已经卸载。

总结

这里按照作者的思路,进行了实践,这个思路确实不错,实践一下,也算吸收了。多谢。这篇文章也可以从我的收藏夹中移除了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值