Skip to content

Proposal: Add new property for getting process command line, environment and parent process #21941

Open

Description

@powercode

Rationale and Usage

When writing tools, it is often useful to be able to get to information about the processes you manage.
This is easy on Linux with /proc, but harder on windows.
When getting parent process, it is important to check the start time of the process with the PID of the queried process to see if it is older to prevent errors when process ids are recycled.

API proposal

namespace System.Diagnostics
{
     public class  Process : Component
     {
           public string CommandLine { get; }
           public IDictionary<string,string> Environment{ get; }
           public Process ParentProcess { get; }
     }
}

I would suggest returning null if the user doesn't have access to the requested property instead of having to handle exceptions.

Activity

  1. rainersigwald commented on Jan 12, 2018

    @rainersigwald
    Member

    We could use this in MSBuild. We attempt to find worker processes to connect to by enumerating running MSBuild processes and then trying to connect to them.

    On Windows + full framework, this is done with Process.GetProcessesByName("MSBuild"), but on core (any OS) the process name tends to be the host . . . so we'll have to try to connect to every dotnet process, instead of filtering to only ones that are running MSBuild. If we had access to the command line, we could be smarter in both cases by attempting to connect only to processes started with the background-process command line parameters.

  2. changed the title [-]Proposal: Add new property for getting process command line and environment and parent process[/-] [+]Proposal: Add new property for getting process command line,environment and parent process[/+] on Jan 18, 2018
  3. changed the title [-]Proposal: Add new property for getting process command line,environment and parent process[/-] [+]Proposal: Add new property for getting process command line, environment and parent process[/+] on Jan 18, 2018
  4. jnm2 commented on Feb 13, 2018

    @jnm2
    Contributor

    IReadOnlyDictionary?

  5. powercode commented on Feb 13, 2018

    @powercode
    Author

    I think a string is better. There may be many nuances to how a command line is split into it's parts, and I think that should be left to some higher level.

  6. jnm2 commented on Feb 13, 2018

    @jnm2
    Contributor

    Unless you're not on Windows. Then to get a single string you have to join the native process args, so you lose information.

  7. powercode commented on Feb 13, 2018

    @powercode
    Author

    In that case, we should be able to do the same thing on windows as .net executables do to get the string[] args in the Main function.

  8. powercode commented on Feb 13, 2018

    @powercode
    Author

    Oh, now I understand that we may be talking about the environment. And in that case IReadOnlyDictionary seams reasonable.

  9. jnm2 commented on Feb 13, 2018

    @jnm2
    Contributor

    Ah, sorry! Yes, IReadOnlyDictionary<string, string> for the environment.
    string for the command line works well on Windows but what about other operating systems? Do we want two properties like is being added for ProcessStartInfo (https://github.com/dotnet/corefx/issues/23592)?

  10. powercode commented on Feb 13, 2018

    @powercode
    Author

    it is a string in /proc/

  11. CJHarmath commented on Sep 12, 2018

    @CJHarmath

    This would also fix PowerShell Get-Process missing the command line argument
    PowerShell/PowerShell#1849

  12. transferred this issue fromdotnet/corefxon Jan 31, 2020
  13. added this to the 5.0 milestone on Jan 31, 2020
  14. modified the milestones: 5.0.0, 6.0.0 on Jun 23, 2020
  15. removed their assignment
    on Aug 12, 2020
  16. modified the milestones: 6.0.0, Future on Jul 26, 2021
  17. danmoseley commented on Sep 2, 2021

    @danmoseley
    Contributor

    @joeloff asked today about getting parent process ID, for an installer scenario. I suppose the argument for returning a Process object vs GetProcessById() is to avoid races with pid recycling.

    I wonder whether it would help to consider environment, command line, and parent as separate requests.

  18. added
    backlog-cleanup-candidate
    on Mar 5, 2026
  19. alexey-leonovich commented on Mar 5, 2026

    @alexey-leonovich

    I'm still interested in this improvement.

  20. removed
    backlog-cleanup-candidate
    on Mar 5, 2026
  21. powercode commented on Mar 5, 2026

    @powercode
    Author

    So am I

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workarea-System.Diagnostics.Process

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions