Welcome to Dream.In.Code
Getting C# Help is Easy!

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!




Getting Process Arguments

 
Reply to this topicStart new topic

Getting Process Arguments

gbertoli3
10 Oct, 2008 - 01:30 PM
Post #1

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
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
User is offlineProfile CardPM
+Quote Post

jacobjordan
RE: Getting Process Arguments
10 Oct, 2008 - 03:40 PM
Post #2

class Me : Perfection
Group Icon

Joined: 11 Jun, 2008
Posts: 1,163



Thanked: 32 times
Dream Kudos: 1625
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Getting Process Arguments
10 Oct, 2008 - 06:24 PM
Post #3

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
Thanks

That sucks that you can't get the arguments.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Getting Process Arguments
10 Oct, 2008 - 06:53 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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();

DataTable queryResults = new DataTable();
queryResults.Columns.Add("Name", Type.GetType("System.String"));
queryResults.Columns.Add("ProcessId", Type.GetType("System.Int32"));
queryResults.Columns.Add("Caption", Type.GetType("System.String"));
queryResults.Columns.Add("Path", Type.GetType("System.String"));
queryResults.Columns.Add("CommandLine", Type.GetType("System.String"));

foreach(ManagementObject obj in runningProcesses)
{
DataRow row = queryResults.NewRow();
row["Name"] = obj["Name"].ToString();
row["ProcessId"] = Convert.ToInt32(obj["ProcessId"]);
if (obj["Caption"]!= null)
row["Caption"] = obj["Caption"].ToString();
if (obj["ExecutablePath"]!= null)
row["Path"] = obj["ExecutablePath"].ToString();
if(obj["CommandLine"] != null)
row["CommandLine"] = obj["CommandLine"].ToString();
queryResults.Rows.Add( row );
}
return queryResults;
}


Hope that helps smile.gif
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Getting Process Arguments
10 Oct, 2008 - 06:58 PM
Post #5

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
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
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Getting Process Arguments
10 Oct, 2008 - 06:58 PM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
By the way, here's a screenshot to show it actually works smile.gif

Attached Image

Look at the screenshot, it's as simple as parsing the string now smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 04:41AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month