可以将名称/值作为参数传递到 Windows 控制台应用程序吗?
构建我的第一个控制台应用程序之一。
该控制台应用程序将运行我定义的一些存储过程。我希望能够通过命令行传入参数值。
有什么方法可以传入名称值对吗?例如:
myConsoleApp.exe sproc_GetLastActives, @LastActiveDate - 11/20/2009
我知道如何检索参数值,但我注意到如果我放入 / 或 , ,args[] 就会被分割。如何传入名称值对?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你想把它放在一个字符串中吗?试试这个:(
即仅添加引号)
斜杠和逗号不应该影响事物,但命令行解析器会按空格分割,除非您引用了它。
Do you want that in a single string? Try this:
(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.
此问题有多种解决方案,最常见的是使用“/”或“-”作为参数前缀,并使用 = 将它们与参数分隔开,例如
在代码中,您可以使用 String.Split(arg[i], new char[] {'='}) 来破坏各个参数。
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
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[]
给出您的 4 个项目,将它们在代码中配对。 :Pconsoleapp.exe key1 val1 key2 val2
args[]
gives your 4 items, pair them up in your code. :P类似于 mykhaylo 的回复,为什么不在一个字符串中传递值:
然后只需测试
=
符号是否存在并解析它以获取键/值对。Similar to mykhaylo's response, why not pass in the values at one string:
Then just test for the presence of the
=
sign and parse it to get your key/value pair.