前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >instsrv.exe srvany.exe启动服务

instsrv.exe srvany.exe启动服务

作者头像
全栈程序员站长
发布2022-09-01 15:36:14
7980
发布2022-09-01 15:36:14
举报

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
复制
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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/141914.html原文链接:https://javaforall.cn

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022年5月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com