大家好,又见面了,我是你们的朋友全栈君。
1.通过注册表注册服务
private static readonly string regpath = @"SYSTEM\CurrentControlSet\Services\ConsulClient\Parameters";
/// <summary>
/// 通过注册表注册Consul Agent服务
/// </summary>
/// <param name="consulPath">exe所在文件夹</param>
/// <param name="consulExeLocal">consul.exe,exe物理路径</param>
private static void ConsulClientRegEdit(string consulPath, string consulExeLocal)
{
try
{
LogWriter.ToInfo("[ConsulClientRegEdit]配置注册表开始");
List<Tuple<string, string, RegistryValueKind>> list = new List<Tuple<string, string, RegistryValueKind>>();
list.Add(new Tuple<string, string, RegistryValueKind>("AppParameters", "agent -config-file conf", RegistryValueKind.String));
list.Add(new Tuple<string, string, RegistryValueKind>("Application", consulExeLocal, RegistryValueKind.String));
list.Add(new Tuple<string, string, RegistryValueKind>("AppDirectory", consulPath, RegistryValueKind.String));
WinRegistryHelper.Register(Registry.LocalMachine, regpath, list);
LogWriter.ToInfo("[ConsulClientRegEdit]配置注册表完成");
}
catch (Exception ex)
{
LogWriter.ToError("[ConsulClientRegEdit]注册表操作失败", ex);
throw new Exception("[ConsulClientRegEdit]注册表操作失败,请联系管理员!");
}
}
public class WinRegistryHelper
{
public static void Register(RegistryKey root, string subkey, List<Tuple<string, string, RegistryValueKind>> list)
{
DeleteRegist(root, subkey);
RegistryKey aimdir = Registry.LocalMachine.CreateSubKey(subkey);
if (aimdir == null)
throw new Exception("注册表操作失败");
foreach (var item in list)
{
aimdir.SetValue(item.Item1, item.Item2, item.Item3);
}
}
/// <summary>
/// 删除注册表中指定的注册表项
/// </summary>
/// <param name="name"></param>
public static void DeleteRegist(RegistryKey root, string subkey)
{
RegistryKey myKey = Registry.LocalMachine.OpenSubKey(subkey);
if (myKey != null)
root.DeleteSubKeyTree(subkey);
}
}
2.instsrv.exe srvany.exe启动服务
string consulExeLocal = DefautToolsRelyHelper.Consul + "\\consul.exe";
string srvanyPath = DefautToolsRelyHelper.Srvany + @"\srvany\srvany.exe";
string instsrvPath = DefautToolsRelyHelper.Srvany + @"\srvany\instsrv.exe";
CheckRelyTools(consulExeLocal, srvanyPath, instsrvPath);
//exe程序注册到注册表中的服务中
ConsulClientRegEdit(DefautToolsRelyHelper.Consul, consulExeLocal);
ConsulClientWinServerRigister(srvanyPath, instsrvPath);
/// <summary>
/// 启动服务
/// </summary>
/// <param name="srvanyPath">srvany.exe物理路径</param>
/// <param name="instsrvPath">instsrv.exe物理路径</param>
private static void ConsulClientWinServerRigister(string srvanyPath, string instsrvPath)
{
LogWriter.ToInfo("[ConsulClientWinServerRigister]使用 srvany注册服务开始");
ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.FileName = "\"" + instsrvPath + "\"";
psInfo.Arguments = consulServiceName + " " + "\"" + srvanyPath + "\"";
//设置启动动作,确保以管理员身份运行
psInfo.Verb = "runas";
Process.Start(psInfo);
LogWriter.ToInfo("[ConsulClientWinServerRigister]使用 srvany注册服务完成");
}
转载于:https://www.cnblogs.com/liuqiyun/p/9897455.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/141914.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...