关于怎么样在vs.net中创建WINDOWS服务程序,以下为一些备忘资料:
用InstallUtil安装Windows服务:
1.打开Visual Studio .NET命令提示
2.改变路径到你项目所在的bin\Debug文件夹位置(如果你以Release模式编译则在bin\Release文件夹)
3.执行命令“InstallUtil.exe MyWindowsService.exe”注册这个服务,使它建立一个合适的注册项。
4.右击桌面上“我的电脑”,选择“管理”就可以打计算机管理控制台
5.在“服务和应用程序”里面的“服务”部分里,你可以发现你的Windows服务已经包含在服务列表当中了
6.右击你的服务选择启动就可以启动你的服务了
在每次需要修改Windows服务时,这就会要求你卸载和重新安装这个服务。不过要注意在卸载这个服务前,最好确保服务管理控制台已经关闭,这会是一个很好的习惯。如果没有这样操作的话,你可能在卸载和重安装Windows服务时会遇到麻烦。仅卸载服务的话,可以执行相的InstallUtil命令用于注销服务,不过要在后面加一个/u命令开关。(InstallUtil.exe MyWindowsService.exe /u)
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
protected override void OnPause()
{
}
protected override void OnContinue()
{
}
|
{
System.ServiceProcess.ServiceBase[] MyServices;
MyServices = new System.ServiceProcess.ServiceBase[] { new Service1(), new Service2() };
System.ServiceProcess.ServiceBase.Run(MyServices);
}
|
private System.Diagnostics.PerformanceCounter fileCreateCounter;
private System.Diagnostics.PerformanceCounter fileDeleteCounter;
private System.Diagnostics.PerformanceCounter fileRenameCounter;
private System.Diagnostics.PerformanceCounter fileChangeCounter;
|
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.fileChangeCounter = new System.Diagnostics.PerformanceCounter();
this.fileDeleteCounter = new System.Diagnostics.PerformanceCounter();
this.fileRenameCounter = new System.Diagnostics.PerformanceCounter();
this.fileCreateCounter = new System.Diagnostics.PerformanceCounter();
fileChangeCounter.CategoryName = "File Monitor Service";
fileDeleteCounter.CategoryName = "File Monitor Service";
fileRenameCounter.CategoryName = "File Monitor Service";
fileCreateCounter.CategoryName = "File Monitor Service";
fileChangeCounter.CounterName = "Files Changed";
fileDeleteCounter.CounterName = "Files Deleted";
fileRenameCounter.CounterName = "Files Renamed";
fileCreateCounter.CounterName = "Files Created";
this.ServiceName = "FileMonitorService";
this.CanPauseAndContinue = true;
this.CanStop = true;
servicePaused = false;
}
|
protected override void OnStart(string[] args)
{
FileSystemWatcher curWatcher = new FileSystemWatcher();
curWatcher.BeginInit();
curWatcher.IncludeSubdirectories = true;
curWatcher.Path =
System.Configuration.ConfigurationSettings.AppSettings
["FileMonitorDirectory"];
curWatcher.Changed += new FileSystemEventHandler(OnFileChanged);
curWatcher.Created += new FileSystemEventHandler(OnFileCreated);
curWatcher.Deleted += new FileSystemEventHandler(OnFileDeleted);
curWatcher.Renamed += new RenamedEventHandler(OnFileRenamed);
curWatcher.EnableRaisingEvents = true;
curWatcher.EndInit();
}
|
private void OnFileChanged(Object source, FileSystemEventArgs e)
{
if( servicePaused == false )
{
fileChangeCounter.IncrementBy(1);
}
}
private void OnFileRenamed(Object source, RenamedEventArgs e)
{
if( servicePaused == false )
{
fileRenameCounter.IncrementBy(1);
}
}
private void OnFileCreated(Object source, FileSystemEventArgs e)
{
if( servicePaused == false )
{
fileCreateCounter.IncrementBy(1);
}
}
private void OnFileDeleted(Object source, FileSystemEventArgs e)
{
if( servicePaused == false )
{
fileDeleteCounter.IncrementBy(1);
}
}
|
protected override void OnStop()
{
if( fileChangeCounter.RawValue != 0 )
{
fileChangeCounter.IncrementBy(-fileChangeCounter.RawValue);
}
if( fileDeleteCounter.RawValue != 0 )
{
fileDeleteCounter.IncrementBy(-fileDeleteCounter.RawValue);
}
if( fileRenameCounter.RawValue != 0 )
{
fileRenameCounter.IncrementBy(-fileRenameCounter.RawValue);
}
if( fileCreateCounter.RawValue != 0 )
{
fileCreateCounter.IncrementBy(-fileCreateCounter.RawValue);
}
}
|
protected override void OnPause()
{
servicePaused = true;
}
protected override void OnContinue()
{
servicePaused = false;
}
|
[RunInstaller(true)]
public class Installer1 : System.Configuration.Install.Installer
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.ServiceProcess.ServiceProcessInstaller spInstaller;
private System.ServiceProcess.ServiceInstaller sInstaller;
public Installer1()
{
// 该调用是设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
#region Component Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
// 创建ServiceProcessInstaller对象和ServiceInstaller对象
this.spInstaller =
new System.ServiceProcess.ServiceProcessInstaller();
this.sInstaller = new System.ServiceProcess.ServiceInstaller();
// 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息
this.spInstaller.Account =
System.ServiceProcess.ServiceAccount.LocalSystem;
this.spInstaller.Username = null;
this.spInstaller.Password = null;
// 设定服务名称
this.sInstaller.ServiceName = "FileMonitorService";
// 设定服务的启动方式
this.sInstaller.StartType =
System.ServiceProcess.ServiceStartMode.Automatic;
this.Installers.AddRange(
new System.Configuration.Install.Installer[]
{this.spInstaller, this.sInstaller });
}
#endregion
}
|
installutil FileMonitorService.exe
|
installutil /u FileMonitorService.exe
|