How to Read Command Line Arguments of Another Process in C#

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"]);

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();
}

}

Get Process's Command Line and arguments from Process object?

Well you could use WMI, there is a class that could be queryied to retrieve the process list and each object contains also a property for the command line that started the process

string query = "SELECT Name, CommandLine, ProcessId, Caption, ExecutablePath " + 
"FROM Win32_Process";
string wmiScope = @"\\your_computer_name\root\cimv2";
ManagementObjectSearcher searcher = new ManagementObjectSearcher (wmiScope, query);
foreach (ManagementObject mo in searcher.Get ())
{
Console.WriteLine("Caption={0} CommandLine={1}",
mo["Caption"], mo["CommandLine"]);
}

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);

How to read from file in command-line arguments otherwise standard in? (emulate Python's fileinput)

stdin is exposed to you as a TextReader through Console.In. Just declare a TextReader variable for your input that either uses Console.In or the file of your choosing and use that for all your input operations.

static TextReader input = Console.In;
static void Main(string[] args)
{
if (args.Any())
{
var path = args[0];
if (File.Exists(path))
{
input = File.OpenText(path);
}
}

// use `input` for all input operations
for (string line; (line = input.ReadLine()) != null; )
{
Console.WriteLine(line);
}
}

Otherwise if refactoring to use this new variable would be too expensive, you could always redirect Console.In to your file using Console.SetIn().

static void Main(string[] args)
{
if (args.Any())
{
var path = args[0];
if (File.Exists(path))
{
Console.SetIn(File.OpenText(path));
}
}

// Just use the console like normal
for (string line; (line = Console.ReadLine()) != null; )
{
Console.WriteLine(line);
}
}

Reading Command Line Arguments of Another Process (Win32 C code)

To answer my own question, I finally found a CodeProject solution that does exactly what I'm looking for:

http://www.codeproject.com/KB/threads/GetNtProcessInfo.aspx

As @Reuben already pointed out, you can use NtQueryProcessInformation to retrieve this information. Unfortuantely it's not a recommended approach, but given the only other solution seems to be to incur the overhead of a WMI query, I think we'll take this approach for now.

Note that this seems to not work if using code compiled from 32bit Windows on a 64bit Windows OS, but since our modules are compiled from source on the target that should be OK for our purposes. I'd rather use this existing code and should it break in Windows 7 or a later date, we can look again at using WMI. Thanks for the responses!

UPDATE: A more concise and C only (as opposed to C++) version of the same technique is illustrated here:

http://wj32.wordpress.com/2009/01/24/howto-get-the-command-line-of-processes/

How to pass parameters to another process in c#

Process p= new Process();
p.StartInfo.FileName = "demo.exe";
p.StartInfo.Arguments = "a b";
p.Start();

or

Process.Start("demo.exe", "a b");

in demo.exe

static void Main (string [] args)
{
Console.WriteLine(args[0]);
Console.WriteLine(args[1]);
}

You asked how to save these params. You can create new class with static properties and save these params there.

class ParamHolder
{
public static string[] Params { get; set;}
}

and in main

static void Main (string [] args)
{
ParamHolder.Params = args;
}

to get params in any place of your program use:

Console.WriteLine(ConsoleParamHolder.Params[0]);
Console.WriteLine(ConsoleParamHolder.Params[1]);

etc.



Related Topics



Leave a reply



Submit