获取服务映像名称

windows服务安装后会在注册表中存储服务信息,路径是HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\[服务名称]

通过ImagePath可以获取到映像名称和服务所在路径,这里的映像名称就是在资源管理器中看到的进程名称,不同于服务名称和显示名称。

   //获取服务路径
private string GetServicePath(string serviceName, string machineName, out string imageName)
{
imageName = string.Empty;
var ret = string.Empty; string registryPath = @"SYSTEM\CurrentControlSet\Services\" + serviceName;
RegistryKey keyHKLM = Registry.LocalMachine; RegistryKey key;
if (string.IsNullOrEmpty(machineName) || machineName == "localhost")
{
key = keyHKLM.OpenSubKey(registryPath); }
else
{
key = RegistryKey.OpenRemoteBaseKey
(RegistryHive.LocalMachine, machineName).OpenSubKey(registryPath);
} string imagePath = key.GetValue("ImagePath").ToString();
key.Close();
var serviceFile = Environment.ExpandEnvironmentVariables(imagePath.Replace("\"", "")); if (serviceFile.IndexOf(".exe", System.StringComparison.CurrentCulture) > )
{
var path = serviceFile.Substring(, serviceFile.IndexOf(".exe") + );
var fileInfo = new FileInfo(path);
imageName = fileInfo.Name;
return new FileInfo(path).DirectoryName;
}
return ret;
}

远程结束进程

Taskkill命令是   taskkill /s 172.19.2.107 /f /t /im "[映像名称]" /U [远程机器的用户名] /P [远程机器的密码]

通过C#调用并获取返回值的方法是:

      /// <summary>
/// 结束服务进程
/// </summary>
/// <param name="imagename"></param>
/// <param name="user"></param>
/// <param name="password"></param>
/// <param name="ip"></param>
/// <returns></returns>
private string TaskKillService(string imagename, string user, string password, string ip)
{ var ret = string.Empty;
var process = new Process();
process.StartInfo.FileName = "taskkill.exe";
process.StartInfo.Arguments = string.Format(" /s {0} /f /t /im \"{1}\" /U {2} /P {3}", ip, imagename, user, password);
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
//process.StartInfo.StandardOutputEncoding = Encoding.UTF8; //process.OutputDataReceived += (s, e) =>
//{
// ret += e.Data;
//};
//process.ErrorDataReceived += (s, e) =>
//{
// ret += e.Data;
//};
//process.BeginOutputReadLine();
//process.BeginErrorReadLine();
process.Start(); ret = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
return ret;
}

服务管理

通过 System.ServiceProcess.ServiceController 也可以管理服务。

  //获取服务状态
private string GetServiceStatus(string serviceName, string ip)
{
try
{
var service = new System.ServiceProcess.ServiceController(serviceName, ip);
return service.Status.ToString();
}
catch (Exception ex)
{
return ex.Message;
}
} //启动服务
private string StartService(string serviceName, string ip)
{
try
{
var service = new System.ServiceProcess.ServiceController(serviceName, ip);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
return "正在运行"; service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds());
return "正在启动";
}
catch (Exception ex)
{
return ex.Message;
}
} //停止服务
private string StopService(string serviceName, string ip)
{
try
{
var service = new System.ServiceProcess.ServiceController(serviceName, ip);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
return "已经停止";
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds());
return "正在停止";
}
catch (Exception ex)
{
return ex.Message;
}
}

远程管理服务需要在本机和远程机之间建立信任凭据

C# 调用 taskkill命令结束服务进程的更多相关文章

  1. C++使用taskkill 命令强制结束进程

    一:查看 taskkill 命令和参数的方法 window系统下,快捷键win + R 打开运行 ,输入cmd回车,在 cmd 里面输入: taskkill /?  二:语法: taskkill [/ ...

  2. tasklist、taskkill命令使用

    tasklist.taskkill命令使用 在Windows XP中新增了两个命令行工具“tasklist.taskkill”.通过“Ctrl+Alt+Del”组合键,打开“任务管理器”就可以查看到本 ...

  3. Perl调用外部命令的方式和区别

    主要的方式简述如下:1. system("command");使用该命令将开启一个子进程执行引号中的命令,父进程将等待子进程结束并继续执行下面的代码. 2. exec(" ...

  4. python 调用shell命令三种方法

    #!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器: #!/usr/bin/env python这种用法是为了防止操作系统用户没有将pyth ...

  5. Perl调用外部命令(其他脚本、系统命令)的方法和区别

    1. `command`; 使用反引号调用外部命令能够捕获其标准输出,并按行返回且每行结束处附带一个回车.反引号中的变量在编译时会被内插为其值.   2. open LIST "ls -l| ...

  6. Java调用本地命令

    参考:http://blog.csdn.net/zhu_xun/article/details/19539513 http://www.cnblogs.com/kingcucumber/p/31801 ...

  7. Python 调用外部命令

    python 可以使用 os 模块来调用外部的 Linux Shell 命令,常用的方法如下: os.system():结果输出在终端上,捕获不到os.popen() : 结果返回一个对象,即标准输出 ...

  8. Android 中调用本地命令

    Android 中调用本地命令 通常来说,在 Android 中调用本地的命令的话,一般有以下 3 种情况: 调用下也就得了,不管输出的信息,比如:echo Hello World.通常来说,这种命令 ...

  9. Python调用cmd命令

    常用的两种方式: 1.python的OS模块. OS模块调用CMD命令有两种方式:os.popen(),os.system(). 都是用当前进程来调用. os.system是无法获取返回值的.当运行结 ...

随机推荐

  1. 【NOI2007】社交网络

    [NOI2007]社交网络 Description 在社交网络(social network)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题.在一个社交圈子里有n个人,人与人之 ...

  2. 安装QConf 报错及解决方案

    1:提示找不到gdbm.h头文件 /alidata/QConf/agent/qconf_dump.cc:1:18: fatal error: gdbm.h: No such file or direc ...

  3. Jmeter 数据库配置池设置IP为参数

    我在网上查了很多资料,发现jmter链接数据库的URL都是设置成固定值的.没有参数化. 当我需要使用配置文件链接不同服务器上的数据库的时候,发现无法实现. 原因在于:jmeter的元件执行优先级是配置 ...

  4. git解决代码提交冲突

    树冲突文件名修改造成的冲突,称为树冲突.比如,A同事把文件改名为A.C,B同事把同一个文件改名为B.C,那么B同事将这两个commit合并时,会产生冲突.如果最终确定用B同事的文件名,那么解决办法如下 ...

  5. MUI的踩坑笔记

    最近在做公司项目的手机端实现,稍微记录下遇到的坑 1.在app开发中,若要使用HTML5+扩展api,必须等plusready事件发生后才能正常使用,mui将该事件封装成了mui.plusReady( ...

  6. 如何利用京东云的对象存储(OSS)上传下载文件

    作者:刘冀 在公有云厂商里都有对象存储,京东云也不例外,而且也兼容S3的标准因此可以利用相关的工具去上传下载文件,本文主要记录一下利用CloudBerry Explorer for Amazon S3 ...

  7. Ruby知识点三:运算符

    1.逻辑运算符 (1)条件1 || 条件2 条件1为假时,才需判断条件2 (2)条件1 && 条件2 条件1为真时,才需判断条件2 2.范围运算符 (1)x..y  从x到y,包括y ...

  8. Linux虚拟机安装教程

    必备组件: vmware(程序主题) 链接:https://pan.baidu.com/s/14OplOGOQTVAnf0iDqgDhDQ 提取码:jape centos(Linux系统) 链接:ht ...

  9. QT中的小细节

    一 .  QT4和QT5的区别(信号和槽):1.  QT4: connect(button,SIGNAL(pressed()),this,SLOT(close())); /** * 优点 :写法简单 ...

  10. Vue+webpack项目中,运行报错Cannot find module 'chalk'的处理

    刚开始用vue + webpack新建项目,在github上下载了一个示例,输入npm init >>>npm run dev 后报错 Cannot find module 'cha ...