-->

vb.net如何使用空格的字符串传递到命令行(vb.net How to pass a string

2019-07-02 18:09发布

我试图使用过程调用外部程序:

    Dim strExe As String = "E:\Projects\Common Files\mktorrent.exe"
    Dim p As New Process
    Dim pinfo As New ProcessStartInfo
    pinfo.UseShellExecute = False
    pinfo.RedirectStandardOutput = True
    pinfo.Arguments = " -a http://blah.com/announce.php -l " & FileSizeMarker & " " & fn
    pinfo.FileName = strExe
    pinfo.WorkingDirectory = fn.Substring(0, fn.LastIndexOf("\"))
    pinfo.WindowStyle = ProcessWindowStyle.Normal
    pinfo.CreateNoWindow = True
    p.StartInfo = pinfo
    p.Start()

问题是与文件名(上述可变FN)。 如果有空格,命令扼流圈 - 没有空格,它工作正常。 我曾尝试加入1,2个或3引号,像这样:

    fn = Chr(34) & Chr(34) & Chr(34) & fn & Chr(34) & Chr(34) & Chr(34)

并且

    fn = "\") & Chr(34) & fn & "\"& Chr(34)

和许多其他的组合,但它仍然给我一个错误。 我如何能得到这个有什么想法与我们合作? TIA

Answer 1:

请检查下面的链接,它在C#中,可能是其对您有所帮助

Word中的命令行参数空间问题



Answer 2:

Windows不提供保持参数用空格作为单个参数的常用方法。 但是也有一些是你已经尝试比较普遍的标准。

所以它归结为两种确定哪些参数处理mktorrent.exe使用,或作为你想传递一个文件名,使用“MSDOS” 8.3格式,这将有没有空格的路径。

对于后者, 这个答案指向Win32API的GetShortPathName

当然,8.3文件名可以用现代的Windows被禁用(所有基于Windows NT的系统,我相信-不在于它经常是)。 所以你唯一的完整的解决方案是,以确定哪些参数处理mktorrent用品。

由于您的评论暗示行情不被通过我经过证实我看到'testing' 'testing' '1 2 3'MsgBox这个VBScript中的输出:

Option Explicit

Dim arg
Dim line

For Each arg in WScript.Arguments
  line = line & " '" & arg & "'"
Next

MsgBox Trim(line)

在使用执行:

Dim strExe As String = "C:\Windows\System32\wscript.exe"
Dim p As New Process
Dim pinfo As New ProcessStartInfo
pinfo.UseShellExecute = False
pinfo.RedirectStandardOutput = True
pinfo.Arguments = " G:\Utils\Arguments.vbs testing ""testing"" ""1 2 3"""
pinfo.FileName = strExe
pinfo.WorkingDirectory = "G:\Utils"
pinfo.WindowStyle = ProcessWindowStyle.Normal
pinfo.CreateNoWindow = True
p.StartInfo = pinfo
p.Start()

所以wscript是看到的报价,并积累了脚本的三个参数。

BTW我只是在让周围的文件名引号修改注意到你的例子尝试fn变量。 你照顾这个与.WorkingDirectory线,应使用未经修改的文件名?



Answer 3:

这让我通过空格键入cmd。 研究小时却一无所获; 这个主题上来不断,希望这将帮助别人。

Dim startprgm As New ProcessStartInfo("cmd.exe", "/C """"C:\Program Files (x86)\Folder\File""""" + strArguments)

请注意,4个引号引起的路径,这部分是非常重要的。 用5个引号导致参数(/℃)不工作,但在后5可分为4和1; 并构造为这样:

Dim startprgm As New ProcessStartInfo("cmd.exe", "/C """"C:\Program Files (x86)""""\Folder\File" + strArguments)

如果打开的cmd.exe,只是发送一条命令,你只需要在路径上的第一次报价(它并不需要被关闭),但VB需要尾随的“关闭”的报价了。

最好的运气,伙计们。



Answer 4:

很简单:

Process.Start("c:\Your exe file", """" & "string with space" & """")


Answer 5:

这真是一个古老的 - 但尚未解决的 - 问题。 我的2美分的贡献。

使用CHR(34)之前和之后的字符串 ,它划定喜欢:

精氨酸= “名称=” &CHR(34)& “John Doe的达席尔瓦” &CHR(34)

只是它!



文章来源: vb.net How to pass a string with spaces to the command line