当前位置: 移动技术网 > IT编程>开发语言>c# > C#开发Windows服务实例之实现禁止QQ运行

C#开发Windows服务实例之实现禁止QQ运行

2019年07月18日  | 移动技术网IT编程  | 我要评论
本实例主要实现下面三个基本功能 1、c#开发windows服务 2、禁止qq等程序运行 3、为windows服务创建自动安装程序 下面针对这三个基本功能进行实现

本实例主要实现下面三个基本功能

1、c#开发windows服务

2、禁止qq等程序运行

3、为windows服务创建自动安装程序

下面针对这三个基本功能进行实现

一、c#开发windows服务

windows服务在vs以前的版本中叫nt服务,在vs.net启用了新的名称。用c#创建windows服务不是一件困难的事,下页针对服务创建、启动、停止做详细介绍

1、首先在vs中添加一winform程序killservice

2、在解决方案添加新项中添加windows服务

3、打开服务页面,切换至代码页面有两个方法如下:

复制代码 代码如下:

protected override void onstart(string[] args)
{
   // todo: 在此处添加代码以启动服务。
}
protected override void onstop()
{
// todo: 在此处添加代码以执行停止服务所需的关闭操作。
}

当服务启动之后一般会要求每隔几秒或者几分钟刷新一次数据,所以要用到一个定时器,在定时器里边进行业务操作。windows服务不能直接在vs下进行调试,所以可以选择使用日志形式记录服务的各种启动停止或者异常的状态。具体实现代码如下:

复制代码 代码如下:

partial class service1 : servicebase
    {
        static system.timers.timer otimer_get = new system.timers.timer();
        public service1()
        {
            initializecomponent();
        }

        protected override void onstart(string[] args)
        {
            // todo: 在此处添加代码以启动服务。
            autolog = false;
            filelog.success("服务已启动");
            otimer_get.enabled = true;
            otimer_get.interval = 10000;
            otimer_get.elapsed += new system.timers.elapsedeventhandler(ontimedevent);
        }
        private void ontimedevent(object sender, system.timers.elapsedeventargs e)
        {
            filelog.success("开始发送");
            otimer_get.enabled = false;
            try
            {
                //此处可进行编写详细的业务操作
            }
            catch (exception ex)
            {
                filelog.error(ex.source + "。" + ex.message);
            }
            otimer_get.enabled = true;
            filelog.success("结束发送");
        }
        protected override void onstop()
        {
            // todo: 在此处添加代码以执行停止服务所需的关闭操作。
            filelog.success("服务已停止");
            otimer_get.enabled = false;
        }
    }

文件记录类代码

复制代码 代码如下:

/// <summary>
    /// 文件型日志记录
    /// </summary>
    public static class filelog
    {
        private static string sfilepath = system.configuration.configurationsettings.appsettings["userlog"];
        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="message">错误内容</param>
        public static void error(string message)
        {
            try
            {
                if (!directory.exists(sfilepath))
                {
                    directory.createdirectory(sfilepath);
                }
                string sfilename = sfilepath + "\\" + string.format("{0}-error.txt", datetime.now.tostring("yyyy-mm-dd"));
                string scontent = string.format("{0}-- {1}\r\n", datetime.now.tostring("yyyy-mm-dd hh:mm:ss"), message);
                filestream fs = new filestream(sfilename, filemode.append);
                byte[] b = encoding.default.getbytes(scontent);
                fs.write(b, 0, b.length);
                fs.close();
            }
            catch { }
        }
        /// <summary>
        /// 正确日志
        /// </summary>
        /// <param name="message">正确内容</param>
        public static void success(string message)
        {
            try
            {
                if (!directory.exists(sfilepath))
                {
                    directory.createdirectory(sfilepath);
                }
                string sfilename = sfilepath + "\\" + string.format("{0}-success.txt", datetime.now.tostring("yyyy-mm-dd"));
                string scontent = string.format("{0}-- {1}\r\n", datetime.now.tostring("yyyy-mm-dd hh:mm:ss"), message);
                filestream fs = new filestream(sfilename, filemode.append);
                byte[] b = encoding.default.getbytes(scontent);
                fs.write(b, 0, b.length);
                fs.close();
            }
            catch { }
        }
    }

4、服务需要一个启动入口,打开program.cs文件在main函数里边编写入口代码如下:

复制代码 代码如下:

static class program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [stathread]
        static void main()
        {
            servicebase[] servicestorun;
            servicestorun = new servicebase[] {
              new service1()
            };
            servicebase.run(servicestorun);
        }
    }

到此windows服务编写完成,但是现在该服务没有什么的业务操作功能。接下来实现禁止本机qq程序运行功能

二、禁止qq等程序运行

只需获取本机所有运行的进行,通过process.kill()方法结束该进程即可

复制代码 代码如下:

process[] process = process.getprocesses();
                for (int i = 0; i < process.length; i++)
                {
                    if (process[i].processname == "qq")
                    {
                        process[i].kill();
                    }
                }

将该操作放至windows服务中的业务操作模块即可。

三、为windows服务创建自动安装程序

上面实现了windows服务基本的业务操作功能,下面为该windows服务创建自动安装程序,

1、切换至service.cs设计页面,右键选择添加安装程序

2、这时项目中就添加了一个新类 projectinstaller 和两个安装组件 serviceprocessinstaller 和 serviceinstaller,并且服务的属性值被复制到组件。

3、若要确定如何启动服务,请右键 serviceinstaller1属性并将 starttype 属性设置为适当的值。

manual      服务安装后,必须手动启动。automatic    每次计算机重新启动时,服务都会自动启动。disabled     服务无法启动。

4、将serviceprocessinstaller类的account属性改为 localsystem这样,不论是以哪个用户登录的系统,服务总会启动。

这些windows服务的安装程序已经完成。通过从生成菜单中选择生成来生成项目。

注意   不要通过按 f5 键来运行项目——不能以这种方式运行服务项目。

5、创建启动和停止文件

安装文件install.bat实现如下:

cd %systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe killservice.exe

net start 自动查杀服务
sc config 自动查杀服务 start= auto 

killservice.exe是你生成的可执行文件的路径

自动查杀服务是windows服务的名称

停止文件uninstall.bat文件的实现如下:

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe /u killservice.exe

killservice.exe是你生成的可执行文件的路径

ps:当这两个文件保存成功之后,执行时一定要“以管理员身份运行”,否则服务启动出错

如果服务安装成功,可以在资源管理器中的服务查看到正在运行的自动查杀服务。

至此整个实例基本完成

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网