How to determine if starting inside a windows service?

前端 未结 6 786
半阙折子戏
半阙折子戏 2021-02-15 16:37

Currently I\'m checking it in the following way:

if (Environment.UserInteractive)
    Application.Run(new ServiceControllerForm(service));
else
    ServiceBase.R         


        
6条回答
  •  情书的邮戳
    2021-02-15 17:32

    You can check if the process or any of its parent processes are listed as a service:

           var process = System.Diagnostics.Process.GetCurrentProcess();
           var parent = process.Parent();
           var procIsService = process?.IsService;
           var parentIsService = parent?.IsService;
           ...
    
    public static class Extensions
    {
        private static string FindIndexedProcessName(int pid)
        {
            var processName = Process.GetProcessById(pid).ProcessName;
            var processesByName = Process.GetProcessesByName(processName);
            string processIndexdName = null;
    
            for (var index = 0; index < processesByName.Length; index++)
            {
                processIndexdName = index == 0 ? processName : processName + "#" + index;
                var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
                if ((int)processId.NextValue() == pid)
                {
                    return processIndexdName;
                }
            }
    
            return processIndexdName;
        }
    
        private static Process FindPidFromIndexedProcessName(string indexedProcessName)
        {
            var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
            return Process.GetProcessById((int)parentId.NextValue());
        }
    
        public static Process Parent(this Process process)
        {
            return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
        }
    
        public static bool IsService(this Process process)
        {
            using (ManagementObjectSearcher Searcher = new ManagementObjectSearcher(
            "SELECT * FROM Win32_Service WHERE ProcessId =" + "\"" + process.Id + "\""))
            {
                foreach (ManagementObject service in Searcher.Get())
                    return true;
            }
            return false;
        }
    }
    

提交回复
热议问题