using Azylee.Core.DataUtils.CollectionUtils;
using Azylee.Core.DataUtils.StringUtils;
using Azylee.Core.WindowsUtils.AdminUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Azylee.Core.WindowsUtils.CMDUtils
{
///
/// CMD 系统服务工具类
///
public static class CMDServiceTool
{
///
/// 安装服务(安装、运行、设置自启,一条龙服务)
///
/// 服务名
/// 应用程序路径
/// 运行账户信息
///
public static bool Install(string name, string path, WindowsAccountModel account = null)
{
try
{
List createResult = CMDProcessTool.Execute($"sc create {name} binPath= \"{path}\"", account);
List startResult = CMDProcessTool.Execute($"net start {name}", account);
List configResult = CMDProcessTool.Execute($"sc config {name} start= AUTO", account);
if (Ls.Ok(createResult))
{
return createResult.Any(x => x.Contains("成功") || x.Contains("服务已存在"));
}
return false;
}
catch { return false; }
}
///
/// 卸载服务(关闭、删除,一条龙服务)
///
///
/// 运行账户信息
public static bool Uninstall(string name, WindowsAccountModel account = null)
{
try
{
List stopResult = CMDProcessTool.Execute($"sc stop {name}", account);
List deleteResult = CMDProcessTool.Execute($"sc delete {name}", account);
List queryResult = CMDProcessTool.Execute($"sc query {name}");
if (Ls.Ok(queryResult))
{
return queryResult.Any(x => x.Contains("失败") || x.Contains("1060") || x.Contains("服务未安装"));
}
return false;
}
catch { return false; }
}
}
}