0

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

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

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

            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 尚未被重定向或进程尚未启动。'

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

4

1 回答 1

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);
            }
于 2019-07-11T09:10:28.960 回答