c# - Windows 服务的可执行文件的路径

标签 c# .net windows-services

如何从另一个程序获取特定 Windows 服务的可执行文件的路径? 不幸的是,类 ServiceController (System.ServiceProcess) 没有为此提供方法或属性!

最佳答案

始终存在 WMI 类 Win32_Service,如 here 所述,特别是 PathName

这个有效:

ManagementClass mc = new ManagementClass("Win32_Service");
foreach(ManagementObject mo in mc.GetInstances())
{
    if(mo.GetPropertyValue("Name").ToString() == "<Short name of your service>")
    {
        return mo.GetPropertyValue("PathName").ToString().Trim('"');
    }
}

关于c# - Windows 服务的可执行文件的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3071215/

相关文章:

c# - 如何对文本框应用约束。在 C# 中,只能将文本框中写入的特定信息/单词添加到列表框中

c# - 在 C# 构造函数中使用 this()

c# - asp.net 身份表未创建

c# - WCF:在 IIS 和 WIndows 服务中托管的区别

java - 启动 jboss windows 服务以从外部可见

c# - 如何设置 Windows 服务的权限

c# - SQLException 未处理。在 GridView 中查看 Excel 文件

c# - 发布错误: Found multiple publish output files with the same relative path

c# - 如何使用 MSMQ 发送/接收多播消息?

wcf - Windows 服务和 WCF