How to Get Command Line Arguments of Other Processes from .Net/C#

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

This is using all managed objects, but it does dip down into the WMI realm:

private static void Main()
{
foreach (var process in Process.GetProcesses())
{
try
{
Console.WriteLine(process.GetCommandLine());
}
catch (Win32Exception ex) when ((uint)ex.ErrorCode == 0x80004005)
{
// Intentionally empty - no security access to the process.
}
catch (InvalidOperationException)
{
// Intentionally empty - the process exited before getting details.
}

}
}

private static string GetCommandLine(this Process process)
{
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id))
using (ManagementObjectCollection objects = searcher.Get())
{
return objects.Cast<ManagementBaseObject>().SingleOrDefault()?["CommandLine"]?.ToString();
}

}

How to get command line args of a process by id?

you can use wmi to get this kind of info

var q = string.Format("select CommandLine from Win32_Process where ProcessId='{0}'", processId);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(q);
ManagementObjectCollection result = searcher.Get();
foreach (ManagementObject obj in result)
Console.WriteLine("[{0}]", obj["CommandLine"]);

Get command line from calling process

Combining the link from Wapac's comment with the Simon Mourier's answer in this question solved the problem.

Now I have two helper classes:

  1. CommandLineUtilities
  2. ParentProcessUtilities

In my program I just need to call now:

Process process = Process.GetCurrentProcess();
Process parent = ParentProcessUtilities.GetParentProcess(process.Id);
String[] parameters = CommandLineUtilities.getCommandLinesParsed(parent);

Best way to pass command line arguments between processes

There's so many ways to do this, IMHO.

A quick and dirty way would be to anonymous pipes. Just before you quit the second instance, you could make a call over the pipes, passing the command line args (maybe do a string.Join(new []{','}, args) to concatenate them first).

There's so many other ways to do inter-process communications though.

A very simplistic example of anonymous pipes usage is give on MSDN:
https://msdn.microsoft.com/en-us/library/bb546102(v=vs.110).aspx

HTH.

How to read command line arguments of another process in C#?


If you did not use the Start method to start a process, the StartInfo property does not reflect the parameters used to start the process. For example, if you use GetProcesses to get an array of processes running on the computer, the StartInfo property of each Process does not contain the original file name or arguments used to start the process. (source: MSDN)

Stuart's WMI suggestion is a good one:

string wmiQuery = string.Format("select CommandLine from Win32_Process where Name='{0}'", processName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
ManagementObjectCollection retObjectCollection = searcher.Get();
foreach (ManagementObject retObject in retObjectCollection)
Console.WriteLine("[{0}]", retObject["CommandLine"]);

How can I pass command-line arguments to an already-running process?

http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicrosoftVisualBasicdll.aspx

Visual basic dll has a WindowsFormsApplicationBase that has StartupNextInstance event in which you can get the arguments of the second instance and the second instance can kill itself on detecting other instances.

This has been asked already C# : how to - single instance application that accepts new parameters?

C# - Console App - How To Read CLI option arguments

For options that take an argument, you should use the generic Option<T>. Use the type of the argument as the type parameter:

new Option<string>(
name: "--name",
description: "The name of the event you like to fetch."
) {
IsRequired = true
}


Related Topics



Leave a reply



Submit