参数用空格 [英] Arguments with spaces

查看:174
本文介绍了参数用空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递一些参数给一个小程序,我写的。 它是一种程序,预计2个参数。 当我把它称为是这样的:

  D:\ littleProgram.exe D:\测试\文件夹\的test.pdf
 

它工作正常。

但是,当我尝试这样的:

  D:\ littleProgram.exe D:\测试2 \文件夹\的test.pdf
 

据认为是获得3个参数...

我想这样的行情:

  D:\ littleProgram.exeD:\测试2 \文件夹\的test.pdf
 

没有运气。

这是VB code:

 模块模块1
        副主(BYVAL个SARG()作为字符串)
            如果sArgs.Length = 0然后
                ......一些code
            elseif的sArgs.Length = 2那么
                ......一些code
            结束如果
         结束小组
前端模块
 

解决方案

命令行参数是空格分隔。

如果你需要一个参数传递,如文件名,有(或可能有)的空间,你可以在双引号括起来。

的例外是当争吵结束与 \ 在这种情况下,你必须用另一种摆脱这种 \

所以,你的情况,这是你所需要的:

  D:\ littleProgram.exeD:\测试2 \文件夹\\的test.pdf
 

所以,你的code是这样的:

 对于我作为整数= 0〜My.Application.CommandLineArgs.Count  -  1
    的Debug.WriteLine(My.Application.CommandLineArgs(I))
下一个
 

输出:

  D:\测试2 \文件夹\
的test.pdf
 

有一个简单的方法可能是删除尾随斜线,共同使用 Path.Combine 添加目录和文件名或只是通过完​​全限定名作为参数(用双引号)

I would like to pass some arguments to a little program i wrote. Its a program that expects 2 arguments. When i call it like this:

d:\littleProgram.exe d:\test\folder\ test.pdf

It works fine.

But when i try this:

d:\littleProgram.exe d:\test 2\folder\ test.pdf

It thinks is gets 3 arguments...

I tried quotes like this:

d:\littleProgram.exe "d:\test 2\folder\" test.pdf

No luck.

This is the vb code:

Module Module1
        Sub Main(ByVal sArgs() As String)
            If sArgs.Length = 0 Then
                ... some code
            ElseIf sArgs.Length = 2 Then
                ... some code
            End If
         End Sub
End Module

解决方案

Command Line Arguments are Space Delimited.

If you need to pass an argument such as a filename that has (or may have) spaces you can enclose it in double quotes.

The exception to this is when arguments end with \ in which case you have to escape this with another \

So in your case this is what you need:

d:\littleProgram.exe "d:\test 2\folder\\" "test.pdf"

So your code would look like this:

For i As Integer = 0 To My.Application.CommandLineArgs.Count - 1
    Debug.Writeline(My.Application.CommandLineArgs(i))
Next

Output:

d:\test 2\folder\
test.pdf

A simpler approach might be to remove the trailing slash and add the directory and filename together using Path.Combine or just pass the fully qualified name as the argument (enclosed in double quotes)

这篇关于参数用空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆