GetProcessShortName() static private method

static private GetProcessShortName ( String name ) : string
name String
return string
Esempio n. 1
0
        /// <summary>Gets the process name 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 process name for the process if it could be found; otherwise, null.</returns>
        public static string?GetProcessName(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.ProcessName);
                    }
                }
            }
            else
            {
                // local case: do not use performance counter and also attempt to get the matching (by pid) process only

                string?processName = Interop.Kernel32.GetProcessName((uint)processId);
                if (processName is not null)
                {
                    return(NtProcessInfoHelper.GetProcessShortName(processName));
                }
            }

            return(null);
        }