Finding out Windows service's running process name .NET 1.1

前端 未结 6 619
野性不改
野性不改 2020-12-30 08:48

We are using a badly written windows service, which will hang when we are trying to Stop it from code. So we need to find which process is related to that service and kill i

6条回答
  •  旧巷少年郎
    2020-12-30 09:40

    To answer exactly to my question - how to find Process related to some service:

    ManagementObjectSearcher searcher = new ManagementObjectSearcher
      ("SELECT * FROM Win32_Service WHERE DisplayName = '" + serviceName + "'");
    
    foreach( ManagementObject result in searcher.Get() )
    {
      if (result["DisplayName"].ToString().ToLower().Equals(serviceName.ToLower()))
      {
        int iPID = Convert.ToInt32( result["ProcessId"] );
        KillProcessByID(iPID, 1000); //some method that will kill Process for given PID and timeout. this should be trivial
      }
    }
    

    }

提交回复
热议问题