【问题标题】:How to find executable path of a windows service如何找到Windows服务的可执行路径
【发布时间】:2019-07-11 08:53:44
【问题描述】:

我正在尝试查找正在运行的服务的可执行路径,并且我查看了 ServiceBase 并且没有指示路径的属性。 ServiceController offer any kind of help也没有。

ServiceBase []services=ServiceController.GetServices();
IEnumerable<string> paths=services.Select(x=> x. ? );

我也尝试过使用sc qc cmd 命令,但它似乎不适用于特定服务

            Process proc = new Process();
            var info = new ProcessStartInfo();
            info.FileName = "cmd.exe";
            info.Arguments = "sc qc \"[service-name]\" | find \"BINARY_PATH_NAME\"";
            proc.StartInfo = info;
            proc.Start();
            var data = await proc.StandardOutput.ReadToEndAsync();

它抛出错误:

System.InvalidOperationException: 'StandardOut 没有被重定向 或该过程尚未开始。'

有没有办法获取特定服务或所有服务的可执行文件路径?

【问题讨论】:

标签: c# cmd windows-services executable-path


【解决方案1】:

您可以使用 WMI

例如(WMI Code Creator):

            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Service");
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("DisplayName: {0}", queryObj["DisplayName"]);
                    Console.WriteLine("Name: {0}", queryObj["Name"]);
                    Console.WriteLine("PathName: {0}", queryObj["PathName"]);
                    Console.WriteLine("ProcessId: {0}", queryObj["ProcessId"]);
                    Console.WriteLine("-----------------------------------");
                }
            }
            catch (ManagementException me)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + me.Message);
            }

【讨论】:

    猜你喜欢
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 2013-06-09
    • 2012-08-18
    相关资源
    最近更新 更多