C#实现Windows 服务的制作安装和删除[转]

关于C#实现windows服务的制作与安装还有删除
运行Visual Studio.NET,建立一个C#的Windows服务项目。
主程序代码:
以下是引用片段:
ContractedBlock.gifExpandedBlockStart.gifCode
using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.ServiceProcess; 
using System.Threading; 
using System.Windows.Forms; 
namespace CareEye 
ExpandedBlockStart.gifContractedBlock.gif

public class CareEye : System.ServiceProcess.ServiceBase 
ExpandedSubBlockStart.gifContractedSubBlock.gif

private Thread MainThread; 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// 
/// 必需的设计器变量。 
/// 

private System.ComponentModel.Container components = null
public CareEye() 
ExpandedSubBlockStart.gifContractedSubBlock.gif

// 该调用是 Windows.Forms 组件设计器所必需的。 
InitializeComponent(); 
// TODO: 在 InitComponent 调用后添加任何初始化 
MainThread = new Thread(new ThreadStart(ThreadFunc)); 
MainThread.Priority 
= ThreadPriority.Lowest; 
}
 
// 进程的主入口点 
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif

//System.ServiceProcess.ServiceBase[] ServicesToRun; 
// 同一进程中可以运行多个用户服务。若要将 
//另一个服务添加到此进程,请更改下行 
// 以创建另一个服务对象。例如, 
// 
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new CareEye(), new MySecondUserService()}; 
// 
//ServicesToRun = new System.ServiceProcess.ServiceBase[] { new CareEye() }; 
System.ServiceProcess.ServiceBase.Run(new CareEye()); 
}
 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// 
/// 设计器支持所需的方法 - 不要使用代码编辑器 
/// 修改此方法的内容。 
/// 

private void InitializeComponent() 
以下是引用片段:
ExpandedSubBlockStart.gifContractedSubBlock.gif

// 
// CareEye 
// 
this.ServiceName = "CareEye"
}
 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// 
/// 清理所有正在使用的资源。 
/// 

protected override void Dispose(bool disposing) 
ExpandedSubBlockStart.gifContractedSubBlock.gif

if (disposing) 
ExpandedSubBlockStart.gifContractedSubBlock.gif

if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif

components.Dispose(); 
}
 
}
 
base.Dispose(disposing); 
}
 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// 
/// 设置具体的操作,以便服务可以执行它的工作。 
/// 

protected override void OnStart(string[] args) 
ExpandedSubBlockStart.gifContractedSubBlock.gif

// TODO: 在此处添加代码以启动服务。 
MainThread.Start(); 
}
 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**//**//// 
/// 停止此服务。 
/// 

protected override void OnStop() 
ExpandedSubBlockStart.gifContractedSubBlock.gif

// TODO: 在此处添加代码以执行停止服务所需的关闭操作。 
MainThread.Abort(); 
}
 
public static void ThreadFunc() 
ExpandedSubBlockStart.gifContractedSubBlock.gif

int LastHour = DateTime.Now.Hour; 
while (true
ExpandedSubBlockStart.gifContractedSubBlock.gif

System.Threading.Thread.Sleep(
60000); 
if (DateTime.Now.Hour - 1 == LastHour) 
ExpandedSubBlockStart.gifContractedSubBlock.gif

MessageBox.Show(
"为了爱护您的眼睛,请您暂时休息5分钟并向远处眺望!""警告", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly); 
LastHour 
= DateTime.Now.Hour; 
}
 
}
 
}
 
}
 
}

添加安装组件:
在设计页面上点右键,出现菜单后,选择添加安装程序。这时会出现一个新的页面,页面上有个控件 serviceProcessInstaller1和serviceInstaller1
serviceProcessInstaller1中把属性Account改为LocalSystem
在把serviceInstaller1中把属性Parent 改为serviceProcessInstaller1 ServiceName属性是管生成服务后的名子
添加完成之后,生成一下(假设名为W2.exe)。到相应的文件夹找到生成的exe文件,找到时会发现有两个.exe用名子比较短的那个。把这个文件拷到一个好记的文件夹中如F盘根目录。
这时就是要把个服务安装一下。进入cmd中的画面,进入Framework2.0的文件如:
cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
后在打
InstallUtil f:\w2.exe 这个就安装了服务 卸载服务是 InstallUtil f:\w2.exe -u

现在就剩启动服务了,
windows服务里启动你安装的服务就可以了。

转载于:https://www.cnblogs.com/qiangshu/archive/2009/11/25/1610228.html