日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 调用 taskkill命令结束服务进程

發(fā)布時間:2025/5/22 C# 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 调用 taskkill命令结束服务进程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

獲取服務(wù)映像名稱

windows服務(wù)安裝后會在注冊表中存儲服務(wù)信息,路徑是HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\[服務(wù)名稱]

通過ImagePath可以獲取到映像名稱和服務(wù)所在路徑,這里的映像名稱就是在資源管理器中看到的進程名稱,不同于服務(wù)名稱和顯示名稱。

//獲取服務(wù)路徑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) > 0){var path = serviceFile.Substring(0, serviceFile.IndexOf(".exe") + 4);var fileInfo = new FileInfo(path);imageName = fileInfo.Name;return new FileInfo(path).DirectoryName;}return ret;}

?

遠程結(jié)束進程

Taskkill命令是 ??taskkill?/s?172.19.2.107?/f?/t?/im?"[映像名稱]" /U [遠程機器的用戶名] /P [遠程機器的密碼]

通過C#調(diào)用并獲取返回值的方法是:

/// <summary>/// 結(jié)束服務(wù)進程/// </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;}

服務(wù)管理

通過?System.ServiceProcess.ServiceController 也可以管理服務(wù)。

//獲取服務(wù)狀態(tài)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;}}//啟動服務(wù)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(5));return "正在啟動";}catch (Exception ex){return ex.Message;}}//停止服務(wù)private string StopService(string serviceName, string ip){try{var service = new System.ServiceProcess.ServiceController(serviceName, ip);if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)return "已經(jīng)停止";service.Stop();service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(5));return "正在停止";}catch (Exception ex){return ex.Message;}}

遠程管理服務(wù)需要在本機和遠程機之間建立信任憑據(jù)

?

轉(zhuǎn)載于:https://www.cnblogs.com/zeroes/p/csharp-taskkill-service.html

總結(jié)

以上是生活随笔為你收集整理的C# 调用 taskkill命令结束服务进程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。