Join 136,263 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,195 people online right now. Registration is fast and FREE... Join Now!
I am trying to Retrieve a Process's Arguments. There is no Error but no text shows up in the ListView(processesListView). I have also taken a look at Win32_Process using System.Management, but there is nothing for arguments.
Here is my Code
csharp
private void GetArguments() { System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses(); for (int i = 0; i < processes.GetUpperBound(0); i++) { ListViewItem item = new ListViewItem(processes[i].ProcessName); ListViewItem.ListViewSubItem item2 = new ListViewItem.ListViewSubItem(item, processes[i].StartInfo.Arguments); item.SubItems.Add(item2); processesListView.Items.Add(item); } }
This post has been edited by gbertoli3: 10 Oct, 2008 - 01:32 PM
I too have attempted something like this a bit ago, and it didn't work for me either. I even made a program that was only supposed to accept a few arguments and run without doing anything. I then tested my thing again, and the arguments showed up empty for that process too. I then found out why; the startinfo property does not get the information used to start a process, but sets it. If you want to start a new process, you would use that property to specify all the arguments and stuff you want to run it with, and then call the Process.Start() method. However, it will not get the starting arguments for an already running process.
Actually you can get the command line arguments that were used to start a process. Here's an example that loads a DataSet with all the running processes, including the command line arguments that were used to start it (if they exist). Using WMI is a very powerful tool
csharp
public static DataTable GetRunningProcesses() { //One way of constructing a query string wmiClass = "Win32_Process"; string condition = ""; string[] queryProperties = new string[] { "Name", "ProcessId", "Caption", "ExecutablePath", "CommandLine" }; SelectQuery wmiQuery = new SelectQuery(wmiClass, condition, queryProperties); ManagementScope scope = new System.Management.ManagementScope(@"\\.\root\CIMV2");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, wmiQuery); ManagementObjectCollection runningProcesses = searcher.Get();
Sorry PsychoCoder that wasn't what I was looking for, I have already tried that. It worked, but it's not what I wanted. I wanted the arguments, not the command line.
Example of arguments I want:
arguments
-a -k -r
EDIT: For some reason it is working now, but not when I first tried it. Thanks PsychoCoder!
This post has been edited by gbertoli3: 10 Oct, 2008 - 06:59 PM