Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 25th, 2007, 09:04 AM
Authorized User
 
Join Date: Jun 2004
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!
 
Old June 25th, 2007, 08:55 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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
 
Old June 25th, 2007, 08:59 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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
 
Old June 26th, 2007, 03:05 AM
Authorized User
 
Join Date: Jun 2004
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!
 
Old July 1st, 2007, 10:07 AM
Registered User
 
Join Date: Jul 2007
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.