How to get name of windows service from inside the service itself

后端 未结 2 452
野趣味
野趣味 2021-02-04 08:12

I have a bunch of win services written in .NET that use same exact executable with different configs. All services write to the same log file. However since I use the same .exe

2条回答
  •  -上瘾入骨i
    2021-02-04 09:10

    I use this function in VB

    Private Function GetServiceName() As String
        Try
            Dim processId = Process.GetCurrentProcess().Id
            Dim query = "SELECT * FROM Win32_Service where ProcessId  = " & processId.ToString
            Dim searcher As New Management.ManagementObjectSearcher(query)
            Dim share As Management.ManagementObject
            For Each share In searcher.Get()
                Return share("Name").ToString()
            Next share
        Catch ex As Exception
            Dim a = 0
        End Try
        Return "DefaultServiceName"
    End Function
    

提交回复
热议问题