学习电脑,计算机系统故障维护,电脑技术,电脑知识学习-就上第二电脑网
当前位置: 首页 > 电脑知识 > 电脑基础

笔记本电脑开机无图标创建一个Windows Service 程序

 更新时间: 2019-07-22 23:08:12   作者:第二电脑网   来源:第二电脑网   浏览数:202   我要评论

002pc.com从笔记本电脑开机无图标创建一个Widows Sevice 程序分析来看,对笔记本电脑开机无图标创建一个Widows Sevice 程序的结果。1.新建Widows项目,选择"Widows服务"

002pc.com从笔记本电脑开机无图标创建一个Windows Service 程序分析来看,对笔记本电脑开机无图标创建一个Windows Service 程序的结果。

1.新建Windows项目,选择"Windows服务"类型的项目。

2.在生成的Service1.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.Timers;

using System.IO;

namespace WindowsService1

{

public partial class Service1 : ServiceBase

{

Timer timer;

public Service1() { InitializeComponent(); }

protected override void OnStart(string[] args)

{

timer = new Timer(1000);

timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

timer.Start();

}

protected override void OnStop()

{

timer.Stop();

timer.Dispose();

}

void timer_Elapsed(object sender, ElapsedEventArgs e)

{

string filePath = AppDomain.CurrentDomain.BaseDirectory + "test.txt";

StreamWriter sw = null;

if (!File.Exists(filePath))

{

sw = File.CreateText(filePath);

}

else

{

sw = File.AppendText(filePath);

}

sw.Write("访问时间:" + DateTime.Now.ToString() + Environment.NewLine);

sw.Close();

}

}

}

3.在和Service1.cs这个项目下在新建一个安装程序类Installer1.cs,代码如下:

using System;

using System.Collections;

using System.Collections.Generic;

using System.ComponentModel;

using System.Configuration.Install;

using System.Linq;

namespace WindowsService1

{

[RunInstaller(true)]

public partial class Installer1 : Installer

{

private System.ServiceProcess.ServiceProcessInstaller spInstaller;

private System.ServiceProcess.ServiceInstaller sInstaller;

public Installer1()

{

InitializeComponent();

// 创建ServiceProcessInstaller对象和ServiceInstaller对象

this.spInstaller = new System.ServiceProcess.ServiceProcessInstaller();

this.sInstaller = new System.ServiceProcess.ServiceInstaller();

// 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息

this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;

this.spInstaller.Password = null;

this.spInstaller.Username = null;

// 设定服务的名称

this.sInstaller.ServiceName = "MyTestWindowsService1";

//设定服务启动的方式

this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.spInstaller, this.sInstaller });

}

}

}

4.生成工程,在bin目录下会生成exe文件。如果直接运行exe文件的话,是不能执行的,需要使用安装Windows服务用到一个名为InstallUtil.exe的命令行工具,打开命令行工具,转到InstallUtil.exe的目录下,对应的目录为:C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe,然后执行InstallUtil.exe+待执行的exe文件的目录,如:InstallUtil.exe F:\MyProject\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe。执行成功后,会在Windows的服务中,出现了刚刚添加的服务的名称。

打开Windows服务列表方式:运行 --> 输入services.msc

在列表中查找一个叫MyTestWindowsService1 的服务,这个就是你创建安装的服务

5.启动该服务,这时打开bin\Debug文件夹,发现已经生成了一个test.txt的文件,里面记录了时间。这说明服务已经正式开始执行。

6.卸载服务的操作也和简单,打开命令行工具,转到C:\Windows\Microsoft.NET\Framework\v4.0.30319目录,然后执行InstallUtil.exe -u F:\MyProject\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe命令就可以了。


更多:笔记本电脑开机无图标创建一个Windows Service 程序
https://www.002pc.com/diannaojichu/112.html

你可能感兴趣的Windows,Service,创建,程序,一个

    关于我们 - 广告合作 - 联系我们 - 免责声明 - 网站地图 - 投诉建议 - 在线投稿

      浙ICP备140365454号

    ©CopyRight 2008-2020 002pc.COM Inc All Rights Reserved. 第二电脑网 版权所有 联系QQ:282523118