diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs index a1ccddef5fb79..b7e925f535a98 100644 --- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs +++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs @@ -1343,15 +1343,30 @@ private void StopWatchingForExit() public override string ToString() { - if (Associated && !HasExited) + string result = base.ToString(); + + try { - string processName = ProcessName; - if (processName.Length != 0) + if (Associated) { - return string.Format(CultureInfo.CurrentCulture, "{0} ({1})", base.ToString(), processName); + _processInfo ??= ProcessManager.GetProcessInfo(_processId, _machineName); + if (_processInfo is not null) + { + string processName = _processInfo.ProcessName; + if (processName.Length != 0) + { + result = $"{result} ({processName})"; + } + } } } - return base.ToString(); + catch + { + // Handle cases where a process would exit straight after our checks + // and/or make ProcessManager throw an exception. + } + + return result; } ///