.NET服务安装、卸载、启动、停止、判断是否存在

一、安装服务:
private void InstallService(IDictionary stateSaver, string filepath)
{
try
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController("ServiceName");
if(!ServiceIsExisted("ServiceName"))
{
//Install Service
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path =filepath;
myAssemblyInstaller.Install(stateSaver);
myAssemblyInstaller.Commit(stateSaver);
myAssemblyInstaller.Dispose();
//--Start Service
service.Start();
}
else
{
if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
{
service.Start();
}
}
}
catch (Exception ex)
{
throw new Exception("installServiceError/n" + ex.Message);
}
}

二、卸载windows服务:
private void UnInstallService(string filepath)
{
try
{
if (ServiceIsExisted("ServiceName"))
{
//UnInstall Service
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path = filepath;
myAssemblyInstaller.Uninstall(null);
myAssemblyInstaller.Dispose();
}
}
catch (Exception ex)
{
throw new Exception("unInstallServiceError/n" + ex.Message);
}
}

三、判断window服务是否存在:
private bool ServiceIsExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
return true;
}
}
return false;
}

四、启动服务:
private void StartService(string serviceName)
{
if (ServiceIsExisted(serviceName))
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
{
service.Start();
for (int i = 0; i < 60; i++)
{
service.Refresh();
System.Threading.Thread.Sleep(1000);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
break;
}
if (i == 59)
{
throw new Exception(startServiceError.Replace("$s$", serviceName));
}
}
}
}
}

五、停止服务:
private void StopService(string serviceName)
{
if (ServiceIsExisted(serviceName))
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
service.Stop();
for (int i = 0; i < 60; i++)
{
service.Refresh();
System.Threading.Thread.Sleep(1000);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
{
break;
}
if (i == 59)
{
throw new Exception(stopServiceError.Replace("$s$", serviceName));
}
}
}
}
}

注:手动安装window服务的方法:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.NET Framework 4.5卸载工具是一个由微软提供的工具,用于卸载.NET Framework 4.5版本。在一些情况下,用户可能需要卸载该版本的.NET Framework,比如出现了兼容性问题或者需要安装其他版本的.NET Framework。 使用.NET Framework 4.5卸载工具很简单,只需按照以下步骤操作: 1. 首先,下载并运行.NET Framework 4.5卸载工具。可以通过微软官方网站或其他可信的下载平台获得该工具。 2. 运行卸载工具后,它将自动检测并列出您系统中已安装.NET Framework 4.5版本。 3. 在工具界面上,您可以选择要卸载.NET Framework 4.5版本。注意,卸载该版本可能会导致相关的应用程序无法正常运行,因此在进行卸载前请确保您已备份重要数据。 4. 卸载过程可能需要一些时间,视您系统中安装.NET Framework 4.5的数量和大小而定。请耐心等待卸载完成。 5. 卸载成功后,您可以尝试重新安装其他版本的.NET Framework或解决您遇到的问题。 需要注意的是,卸载.NET Framework 4.5可能会对一些应用程序产生影响,因为一些程序可能依赖于.NET Framework 4.5,如果卸载该版本,这些应用程序可能无法正常运行。因此,在卸载之前,请确保事先了解您系统中的应用程序是否依赖于该版本。 总而言之,.NET Framework 4.5卸载工具是一个用于卸载.NET Framework 4.5版本的工具,可以帮助用户解决兼容性问题或进行版本切换。在使用该工具前,请先备份重要数据,并对您系统中的应用程序进行充分了解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值