views:

240

answers:

5

Hello,

Building one of my first console apps.

This console app will run some stored procedures I'm defining. I would like to be able to pass in parameter values via the command line.

Is there any way to pass in a name value pair? For example:

myConsoleApp.exe sproc_GetLastActives, @LastActiveDate - 11/20/2009

I know how to retreive the parameter values, but I'm noticing that the args[] are split if I put in a / or a ,. How can I pass in name value pair?

Thanks!

+1  A: 

Do you want that in a single string? Try this:

myConsoleApp.exe "sproc_GetLastActives, @LastActiveDate - 11/20/2009"

(i.e. just add quotes)

Slashes and commas shouldn't affect things, but the command line parser splits on spaces unless you've quoted it.

Jon Skeet
A: 

consoleapp.exe key1 val1 key2 val2

args[] gives your 4 items, pair them up in your code. :P

o.k.w
A: 

Create a file in which you are detained application settings:

LastActiveDate=11/20/2009
DEBUG=TRUE
KEY0=VALUE0

Start-up:

consoleapp.exe file_name
mykhaylo
A: 

Similar to mykhaylo's response, why not pass in the values at one string:

consoleapp.exe sproc_name key1=value1 "key2=Value With Spaces" key3=value3

Then just test for the presence of the = sign and parse it to get your key/value pair.

Agent_9191
+1  A: 

There are several solutions to this problem the most common is to use '/' or '-' to prefix parameters and = to delimit them from their arguments for example

consoleapp.exe /spname=sproc_GetLastActives /LastActiveDate="11/20/2009"

in your code your code you can use String.Split(arg[i], new char[] {'='}) to bust up the individual parameters.

Jeremy E