如何找到 windows 服务的可执行路径

How to find executable path of a windows service

我正在尝试查找 运行 服务的可执行路径,我查看了 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 has not been redirected or the process hasn't started yet.'

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

您可以使用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);
            }