Created
January 17, 2020 21:09
-
-
Save mubix/a8882940311d511dfe0e598e5a3fd1a8 to your computer and use it in GitHub Desktop.
Get a process listing with the command line arguments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Diagnostics; | |
using System.Management; | |
namespace GetProcessList | |
{ | |
public static class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Process[] processlist = Process.GetProcesses(); | |
foreach(Process theprocess in processlist) | |
{ | |
Console.WriteLine("Process: {0} ID: {1} CmdLine: {2}", theprocess.ProcessName, theprocess.Id, GetCommandLine(theprocess)); | |
} | |
} | |
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(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment