栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Windows服务如何确定其ServiceName?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Windows服务如何确定其ServiceName?

来自:https
:
//connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=387024

这是WMI解决方案。覆盖 Servicebase.ServiceMainCallback() 可能也可以,但是这似乎对我有用。

    protected String GetServiceName()    {        // Calling System.ServiceProcess.Servicebase::ServiceNamea allways returns        // an empty string,        // see https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=387024        // So we have to do some more work to find out our service name, this only works if        // the process contains a single service, if there are more than one services hosted        // in the process you will have to do something else        int processId = System.Diagnostics.Process.GetCurrentProcess().Id;        String query = "SELECt * FROM Win32_Service where ProcessId = " + processId;        System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(query);        foreach (System.Management.ManagementObject queryObj in searcher.Get()) { return queryObj["Name"].ToString();        }        throw new Exception("Can not get the ServiceName");    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/465330.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号