using System;
using System.Collections;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
namespace AutoInstallUtil
{
public class SystemServices
{
/// <summary>
/// 打开系统服务
/// </summary>
/// <param name="serviceName">系统服务名称</param>
/// <returns></returns>
public static bool SystemServiceOpen(string serviceName)
{
try
{
using (var control = new ServiceController(serviceName))
{
if (control.Status != ServiceControllerStatus.Running)
{
control.Start();
}
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 关闭系统服务
/// </summary>
/// <param name="serviceName">系统服务名称</param>
/// <returns></returns>
public static bool SystemServiceClose(string serviceName)
{
try
{
using (var control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Running)
{
control.Stop();
}
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 重启系统服务
/// </summary>
/// <param name="serviceName">系统服务名称</param>
/// <returns></returns>
public static bool SystemServiceReStart(string serviceName)
{
try
{
using (var control = new ServiceController(serviceName))
{
if (control.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
control.Continue();
}
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 返回服务状态
/// </summary>
/// <param name="serviceName">系统服务名称</param>
/// <returns>1:服务未运行 2:服务正在启动 3:服务正在停止 4:服务正在运行 5:服务即将继续 6:服务即将暂停 7:服务已暂停 0:未知状态</returns>
public static int GetSystemServiceStatus(string serviceName)
{
try
{
using (var control = new ServiceController(serviceName))
{
return (int)control.Status;
}
}
catch
{
return 0;
}
}
/// <summary>
/// 返回服务状态
/// </summary>
/// <param name="serviceName">系统服务名称</param>
/// <returns>1:服务未运行 2:服务正在启动 3:服务正在停止 4:服务正在运行 5:服务即将继续 6:服务即将暂停 7:服务已暂停 0:未知状态</returns>
public static string GetSystemServiceStatusString(string serviceName)
{
try
{
using (var control = new ServiceController(serviceName))
{
var status = string.Empty;
switch ((int)control.Status)
{
case 1:
status = "服务未运行";
break;
case 2:
status = "服务正在启动";
break;
case 3:
status = "服务正在停止";
break;
case 4:
status = "服务正在运行";
break;
case 5:
status = "服务即将继续";
break;
case 6:
status = "服务即将暂停";
break;
case 7:
status = "服务已暂停";
break;
case 0:
status = "未知状态";
break;
}
return status;
}
}
catch
{
return "未知状态";
}
}
/// <summary>
/// 安装服务
/// </summary>
/// <param name="stateSaver"></param>
/// <param name="filepath"></param>
public static void InstallService(IDictionary stateSaver, string filepath)
{
try
{
var myAssemblyInstaller = new AssemblyInstaller
{
UseNewContext = true,
Path = filepath
};
myAssemblyInstaller.Install(stateSaver);
myAssemblyInstaller.Commit(stateSaver);
myAssemblyInstaller.Dispose();
}
catch (Exception ex)
{
throw new Exception("installServiceError/n" + ex.Message);
}
}
public static bool ServiceIsExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
return services.Any(s => s.ServiceName == serviceName);
}
/// <summary>
/// 卸载服务
/// </summary>
/// <param name="filepath">路径和文件名</param>
public static void UnInstallService(string filepath)
{
try
{
//UnInstall Service
var myAssemblyInstaller = new AssemblyInstaller
{
UseNewContext = true,
Path = filepath
};
myAssemblyInstaller.Uninstall(null);
myAssemblyInstaller.Dispose();
}
catch (Exception ex)
{
throw new Exception("unInstallServiceError/n" + ex.Message);
}
}
}
}
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace AutoInstallUtil
{
class Program
{
static void Main(string[] args)
{
try
{
ServerAction();
}
catch (Exception ex)
{
Console.WriteLine("发生错误:{0}", ex.Message);
}
Console.ReadKey();
}
/// <summary>
/// 操作
/// </summary>
private static void ServerAction()
{
Console.WriteLine("请输入:1安装 2卸载");
var condition = Console.ReadLine();
var currentPath = Environment.CurrentDirectory;
var currentFileNameVshost = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName).ToLower();
var currentFileName = currentFileNameVshost.Replace(".vshost.exe", ".exe");
var files =
Directory.GetFiles(currentPath)
.Select(o => Path.GetFileName(o).ToLower())
.ToList()
.Where(
o =>
o != currentFileNameVshost
&& o != currentFileName
&& o.ToLower().EndsWith(".exe")
&& o != "installutil.exe"
&& !o.ToLower().EndsWith(".vshost.exe"))
.ToList();
if (files.Count == 0)
{
Console.WriteLine("未找到可执行文件,请确认当前目录有需要安装的服务程序");
}
else
{
Console.WriteLine("找到目录有如下可执行文件,请选择需要安装或卸载的文件序号:");
}
int i = 0;
foreach (var file in files)
{
Console.WriteLine("序号:{0} 文件名:{1}", i, file);
i++;
}
var serviceFileIndex = Console.ReadLine();
var servicePathName = currentPath + "\\" + files[Convert.ToInt32(serviceFileIndex)];
if (condition == "1")
{
SystemServices.InstallService(null, servicePathName);
}
else
{
SystemServices.UnInstallService(servicePathName);
}
Console.WriteLine("**********本次操作完毕**********");
ServerAction();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有