English 中文(简体)
Possible to pass name/value as parameter into Windows Console Application?
原标题:

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!

问题回答

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.

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.

consoleapp.exe key1 val1 key2 val2

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

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签