vb.net 如何将带空格的字符串传递给命令行 [英] vb.net How to pass a string with spaces to the command line

查看:16
本文介绍了vb.net 如何将带空格的字符串传递给命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Process 调用外部程序:

I am trying to call an external program using Process:

    Dim strExe As String = "E:ProjectsCommon Filesmktorrent.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 个引号,如下所示:

The problem is with the filename (variable fn above). If it has spaces, the command chokes - without spaces, it works fine. I have tried adding 1, 2 or3 quotes, like this:

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

还有

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

和许多其他组合,但它仍然给我一个错误.关于如何让它发挥作用的任何想法?TIA

and many other combinations, but it still gives me an error. Any thoughts on how I can get this to work? TIA

推荐答案

这确实是一个古老但尚未解决的问题.我的 2 美分贡献.

It's really an old - but unsolved - problem. My 2 cents of contribution.

在字符串前后使用CHR(34),像这样分隔:

Use CHR(34) before-and-after the string, delimiting it like:

Arg = "Name=" &chr(34) &约翰·多伊·达席尔瓦"&chr(34)

Arg = "Name=" & chr(34) & "John Doe da Silva" & chr(34)

就这样!

这篇关于vb.net 如何将带空格的字符串传递给命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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