本质上,你需要做一个服务installer和一个服务控制器。 前一个很简单,在服务的[设计]界面上,右击空白处,选“添加安装程序”。后面自己搞吧,很简单。 服务控制器:class CServiceController { private ServiceController m_watcherSC = null; public bool start() { if (m_watcherSC.Status == ServiceControllerStatus.Stopped) { // Start the service, and wait until its status is "Running". m_watcherSC.Start(); m_watcherSC.WaitForStatus(ServiceControllerStatus.Running); } return true; } public bool stop() { if (m_watcherSC.Status == ServiceControllerStatus.Running) { // Start the service, and wait until its status is "Stopped". m_watcherSC.Stop(); m_watcherSC.WaitForStatus(ServiceControllerStatus.Stopped); } return true; } public void setControlleeName(string name) { m_watcherSC = new ServiceController(name); } public string getControlleeStatus() { return getServiceStatus(m_watcherSC); } /// <summary> /// 获取服务的状态,以中文形式输出。 /// </summary> /// <param name="sc">服务</param> /// <returns>服务状态</returns> private string getServiceStatus(ServiceController sc) { string status = ""; switch (sc.Status) { case ServiceControllerStatus.Running: status = sc.ServiceName + "服务已运行。"; break; case ServiceControllerStatus.Stopped: status = sc.ServiceName + "服务已停止。"; break; case ServiceControllerStatus.StopPending: status = sc.ServiceName + "服务正在停止。"; break; case ServiceControllerStatus.Paused: status = sc.ServiceName + "服务已暂停。"; break; case ServiceControllerStatus.StartPending: status = sc.ServiceName + "服务正在启动。"; break; } return status; } }
System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController();
serviceController.ServiceName = "";
serviceController.Start();//启动
serviceController.Stop();//停止服务中: protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
timer1.Interval = 10000;//10秒检测一次
timer1.Enabled = true;
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Process[] ps=Process.GetProcessesByName( "winform1");
if(ps.Length==0)
{
Process.Start( "winform1.exe ");
}
}服务的StartType属性改成Automatic,自动启动
是的,服务的目录就是那个。
发布服务的方法有几种,我常用的就是 切换到服务的视图界面,右击 添加安装程序,多出来一个ProjectInstaller.cs, 然后在视图界面上把serviceInstaller1的Desription(服务的描述改下),
serviceProcessInstaller1的Account改成LocalSystem。。然后重新生成。cmd命令:
cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727InstallUtil -u (拆卸服务)
E:\yn_sound\ServerForMoveFile\ServerForMoveFile\ServerForMoveFile\bin\Debug\ServerForMoveFile.exe
InstallUtil (安装服务)E:\yn_sound\ServerForMoveFile\ServerForMoveFile\ServerForMoveFile\bin\Debug\ServerForMoveFile.exe
如果是权限问题的话,serviceProcessInstaller1的Account改成LocalSystem就是把服务的运行账户改成本地系统。。你试试改下 服务的运行账户 权限。应该就能解决
app.manifest修改如下:
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
不过,我修改之后 出现了 ClickOnce 不支持请求执行级别“requireAdministrator”
的错误。
win7的权限控制是挺烦的。我了解不多。。win7好像可以把exe改成始终用管理员身份运行吧。
你参考下这个。http://www.cnblogs.com/likewind/archive/2010/10/20/1856604.html
前一个很简单,在服务的[设计]界面上,右击空白处,选“添加安装程序”。后面自己搞吧,很简单。
服务控制器:class CServiceController {
private ServiceController m_watcherSC = null;
public bool start()
{
if (m_watcherSC.Status == ServiceControllerStatus.Stopped)
{
// Start the service, and wait until its status is "Running".
m_watcherSC.Start();
m_watcherSC.WaitForStatus(ServiceControllerStatus.Running);
}
return true;
} public bool stop()
{
if (m_watcherSC.Status == ServiceControllerStatus.Running)
{
// Start the service, and wait until its status is "Stopped".
m_watcherSC.Stop();
m_watcherSC.WaitForStatus(ServiceControllerStatus.Stopped);
}
return true;
} public void setControlleeName(string name)
{
m_watcherSC = new ServiceController(name);
} public string getControlleeStatus()
{
return getServiceStatus(m_watcherSC);
}
/// <summary>
/// 获取服务的状态,以中文形式输出。
/// </summary>
/// <param name="sc">服务</param>
/// <returns>服务状态</returns>
private string getServiceStatus(ServiceController sc)
{
string status = "";
switch (sc.Status)
{
case ServiceControllerStatus.Running:
status = sc.ServiceName + "服务已运行。";
break;
case ServiceControllerStatus.Stopped:
status = sc.ServiceName + "服务已停止。";
break;
case ServiceControllerStatus.StopPending:
status = sc.ServiceName + "服务正在停止。";
break;
case ServiceControllerStatus.Paused:
status = sc.ServiceName + "服务已暂停。";
break;
case ServiceControllerStatus.StartPending:
status = sc.ServiceName + "服务正在启动。";
break;
}
return status;
}
}