来自: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"); }


