C#
.NET
command line arguments
process management
inter-process communication

Can I get command line arguments of other processes from .NET/C?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, but not through a simple built-in cross-platform .NET API. System.Diagnostics.Process can tell you a lot about another process, but command-line arguments are operating-system-specific and often protected by permissions. In practice, you read them through platform facilities such as WMI on Windows or /proc on Linux.

Windows approach with WMI

On Windows, a common solution is querying the Win32_Process class and reading its CommandLine property.

csharp
1using System;
2using System.Management;
3
4class Program
5{
6    static void Main()
7    {
8        int pid = 1234;
9        string query = $"SELECT CommandLine FROM Win32_Process WHERE ProcessId = {pid}";
10
11        using var searcher = new ManagementObjectSearcher(query);
12        foreach (ManagementObject obj in searcher.Get())
13        {
14            Console.WriteLine(obj["CommandLine"]);
15        }
16    }
17}

This is straightforward for administrative tools and diagnostics. You need a reference to System.Management, and the code works only on Windows.

Permissions matter. Some processes, especially system or elevated ones, may deny access unless your program has sufficient privileges.

Linux approach with /proc

On Linux, the usual source is /proc/<pid>/cmdline. That file contains the command line as NUL-separated strings.

csharp
1using System;
2using System.IO;
3
4class Program
5{
6    static void Main()
7    {
8        int pid = 1234;
9        string path = $"/proc/{pid}/cmdline";
10
11        string raw = File.ReadAllText(path);
12        string[] args = raw.Split('\0', StringSplitOptions.RemoveEmptyEntries);
13
14        Console.WriteLine(string.Join(" | ", args));
15    }
16}

The first element is usually the executable path or command name, and the remaining elements are the arguments. As on Windows, access depends on process ownership, container isolation, and system configuration.

There is no single portable answer

If your application targets multiple platforms, you usually branch by operating system and use the native mechanism for each one.

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        if (OperatingSystem.IsWindows())
8        {
9            Console.WriteLine("Use WMI or another Windows-specific API.");
10        }
11        else if (OperatingSystem.IsLinux())
12        {
13            Console.WriteLine("Read /proc/<pid>/cmdline.");
14        }
15        else
16        {
17            Console.WriteLine("Platform-specific implementation required.");
18        }
19    }
20}

This is one of those cases where the operating system defines the capability much more than .NET does.

On macOS, this is generally more restricted and often requires native platform calls or elevated privileges. That is another reason there is no clean, universal managed API for the job.

When you control the target process

If you own both processes, reading the startup command line is often the wrong integration strategy. A more reliable design is to have the target process expose the data you need through:

  • Named pipes
  • HTTP or gRPC
  • Shared files
  • Environment-specific IPC

That avoids permission problems and keeps you from depending on platform-specific internals.

Common Pitfalls

The first pitfall is assuming .NET should expose a universal GetCommandLine() method for arbitrary processes. The runtime avoids that because the OS rules differ widely.

Another issue is access denial. Even if the API is correct, the operating system may hide command-line data for processes owned by other users or with elevated privileges.

Developers also forget that quoted arguments are already flattened into a platform-specific command line. Reconstructing exactly how the original command was typed can be harder than simply reading the current parsed form.

Finally, do not use this technique as a substitute for proper inter-process communication when you control both programs. It is a diagnostic tool first, not a robust contract.

Summary

  • You can read another process's command line, but the method is OS-specific.
  • On Windows, WMI and Win32_Process.CommandLine are common options.
  • On Linux, /proc/<pid>/cmdline is the standard source.
  • Permissions and process ownership can block access.
  • If both processes are yours, use explicit IPC instead of scraping startup arguments.

Course illustration
Course illustration

All Rights Reserved.