如何让窗口服务phyiscal路径使用.NET? [英] how to get phyiscal path of windows service using .net?

查看:185
本文介绍了如何让窗口服务phyiscal路径使用.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得在.NET管理应用程序窗口服务的绝对路径。我使用的.Net中的ServiceController如下图所示。

I have to get the absolute path of a windows service in a .Net Admin application. I am using ServiceController of .Net as shown below.

ServiceController serviceController = new  ServiceController(serviceName);

不过,我看不出有任何财产这里获取服务的.exe文件的绝对路径。反正是有以编程方式获得此。

But I don't see any property here to get the absolute path of the .exe of the service. Is there anyway to get this programmatically.

推荐答案

您可以得到这个使用WMI:

You can get this using WMI:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(GetPathOfService("eventlog"));
        Console.ReadLine();
    }

    public static string GetPathOfService(string serviceName)
    {
        WqlObjectQuery wqlObjectQuery = new WqlObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", serviceName));
        ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(wqlObjectQuery);
        ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();

        foreach (ManagementObject managementObject in managementObjectCollection)
        {
            return managementObject.GetPropertyValue("PathName").ToString();
        }

        return null;
    }
}

这篇关于如何让窗口服务phyiscal路径使用.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆