System.Diagnostics.NtProcessManager.GetProcessInfos C# (CSharp) Method

GetProcessInfos() public static method

public static GetProcessInfos ( string machineName, bool isRemoteMachine ) : System.Diagnostics.ProcessInfo[]
machineName string
isRemoteMachine bool
return System.Diagnostics.ProcessInfo[]
        public static ProcessInfo[] GetProcessInfos(string machineName, bool isRemoteMachine)
        {
            PerformanceCounterLib library = null;
            try
            {
                library = PerformanceCounterLib.GetPerformanceCounterLib(machineName, new CultureInfo("en"));
                return GetProcessInfos(library);
            }
            catch (Exception e)
            {
                if (isRemoteMachine)
                {
                    throw new InvalidOperationException(SR.CouldntConnectToRemoteMachine, e);
                }
                else
                {
                    throw e;
                }
            }
            // We don't want to call library.Close() here because that would cause us to unload all of the perflibs.
            // On the next call to GetProcessInfos, we'd have to load them all up again, which is SLOW!
        }

Same methods

NtProcessManager::GetProcessInfos ( PerformanceCounterLib library ) : System.Diagnostics.ProcessInfo[]
NtProcessManager::GetProcessInfos ( PerformanceCounterLib library, int processIndex, int threadIndex, byte data ) : System.Diagnostics.ProcessInfo[]

Usage Example

        /// <summary>Gets the ProcessInfo for the specified process ID on the specified machine.</summary>
        /// <param name="processId">The process ID.</param>
        /// <param name="machineName">The machine name.</param>
        /// <returns>The ProcessInfo for the process if it could be found; otherwise, null.</returns>
        public static ProcessInfo GetProcessInfo(int processId, string machineName)
        {
            if (IsRemoteMachine(machineName))
            {
                // remote case: we take the hit of looping through all results
                ProcessInfo[] processInfos = NtProcessManager.GetProcessInfos(machineName, isRemoteMachine: true);
                foreach (ProcessInfo processInfo in processInfos)
                {
                    if (processInfo.ProcessId == processId)
                    {
                        return(processInfo);
                    }
                }
            }
            else
            {
                // local case: do not use performance counter and also attempt to get the matching (by pid) process only
                ProcessInfo[] processInfos = NtProcessInfoHelper.GetProcessInfos(pid => pid == processId);
                if (processInfos.Length == 1)
                {
                    return(processInfos[0]);
                }
            }

            return(null);
        }
All Usage Examples Of System.Diagnostics.NtProcessManager::GetProcessInfos