Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 25th, 2007, 09:04 AM
Authorized User
 
Join Date: Jun 2004
Location: Darmstadt, , Germany.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Finding commandline arguments of a running process

Does anybody know how to find what commandline arguments were given when a process was started? Preferably without using WMI, which is a little slow.

Anybody who refers to static void Main ( string [] args ) in their answer gets shot - I mean, of course, a process other than the one which wants to know what the arguments are/were ;)

Ed.
Ne auderis delere orbem rigidum meum!
__________________
Ed.
Ne auderis delere orbem rigidum meum!
Reply With Quote
  #2 (permalink)  
Old June 25th, 2007, 08:55 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Now you are just asking for trouble! ;)

Of course, the answer is....

    static void Main(string[] args)

What else do you expect? I have seem some argument "frameworks" (for lack of a better term) that simplify dealing with the args. But ultimately in the end, you simply have to parse the array and look for all the things you expect to find.

I'm not sure why MS has not made it a little easier to deal with this but I guess the "argument" (har har) would be that you are going from a very uncontrollable world (the command line) to a very controlled world (managed .NET). Short or handling the parsing of the command line by giving you an array of space delimited values you have to figure out what it all means on your own.

Try googling for c# command line argument helpers or something similar. I have used such a helper but can't remember for the life of me where I found it. It was a long time ago that I used it.

-Peter
Reply With Quote
  #3 (permalink)  
Old June 25th, 2007, 08:59 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

After re-reading your original post again, I think I got your meaning.

I don't think you can. I imagine only the process that was started from the command line can get at the command args (based on the protection level of the args array on the static method. But I suppose you could just drop the array to a private var and expose it to the world. Would that work?

(Sorry for the long winded off-topic reply, I got myself going when I thought up the bad pun.)

-Peter
Reply With Quote
  #4 (permalink)  
Old June 26th, 2007, 03:05 AM
Authorized User
 
Join Date: Jun 2004
Location: Darmstadt, , Germany.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the replies, I will forgive your awful pun

I'm sure there is a way... Process Explorer (www.sysinternals.com) is able to find the arguments of a running process, and my colleague has seen a way which uses WMI but it's very slow and I was hoping there might be a faster way as I'm quite new to C#.
Naturally, I've been searching the world's largest textbook (google) but no luck so far. There are a couple of examples of using WMI at these sites:
http://www.dotnet247.com/247reference/msgs/7/35098.aspx
http://www.codeproject.com/cs/system...select=1274879

but as I said, it's a little slow. It's also very ugly, and I was hoping there might be a "cleaner" C# solution than using what appears to be SQL queries.

Ed.
Ne auderis delere orbem rigidum meum!
Reply With Quote
  #5 (permalink)  
Old July 1st, 2007, 10:07 AM
Registered User
 
Join Date: Jul 2007
Location: Islamabad, NWFP, Pakistan.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There is a way other than
Code:
public static void main (string[] a)
which is parsing
Code:
System.Environment.CommandLine;
but I don't think this would be helpful in what you want to do.

.pk
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get running Instance of a Process not in ROT? scando C# 1 February 20th, 2018 01:06 PM
process.startinfo opens new instance of process Anypond General .NET 0 August 28th, 2008 05:35 AM
take arguments when running program in console iulian C++ Programming 8 January 16th, 2007 09:17 AM
Know about a running process for a given resource baburman General .NET 2 October 13th, 2005 03:04 AM
getting commandline program output r_ganesh76 General .NET 1 November 10th, 2004 11:42 AM



All times are GMT -4. The time now is 10:06 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.