3

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

    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

4

6 回答 6

2

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

在 string 前后使用CHR(34) ,将其分隔为:

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

只是它!

于 2018-07-15T22:27:03.650 回答
1

Please check the below link, its in C#, may be its helpful to you

Word command-line-arguments space issues

于 2012-10-27T08:23:55.937 回答
1

Windows does not provide a common way of keeping arguments with spaces as single arguments. However there are a number of relatively common standards that you've tried.

So it comes down to either determining what argument processing mktorrent.exe uses or, as you're trying to pass a filename, using "MSDOS" 8.3 format for the path which will have no spaces.

For the latter, this answer points to the Win32API GetShortPathName.

Of course, 8.3 filenames can be disabled with modern Windows (all Windows NT-based systems I believe -- not that it often is). So your only full solution is to determine what argument processing mktorrent supplies.

Since your comment suggesting the quotes are not being passed through I confirmed I see 'testing' 'testing' '1 2 3' in the MsgBox output of this vbscript:

Option Explicit

Dim arg
Dim line

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

MsgBox Trim(line)

when executed using:

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()

So wscript is seeing the quotes and is accumulating three arguments for the script.

BTW I just noticed your example attempts at getting quotes around the filename modify the fn variable. Did you cater for this with the .WorkingDirectory line, which should be using the unmodified filename?

于 2012-10-30T06:36:06.483 回答
1

这工作:

Dim current_path, current_rulename, cmd1 as STRING

current_path = "C:\this folder\file name.exe"    
current_rulename = "file name.exe"

cmd1 = "netsh advfirewall firewall add rule name = """ + current_rulename + """ dir = in action = block program = """ + current_path + """"
cmd1 &= " & "
cmd1 &= "netsh advfirewall firewall add rule name = """ + current_rulename + """ dir = out action = block program = """ + current_path + """"
cmd1 &= " & pause"

Process.Start("cmd", "/c " + cmd1)

基本上,带空格的变量需要像这样括起来:

""" + string_with_spaces + """

分成几部分:

cmd1 = 
"
netsh advfirewall firewall add rule name = 
""" + current_rulename + """
dir=in action=block
program=
""" + current_path + """
"

此代码连接了两个使用带空格的字符串的单独命令。

于 2019-08-22T00:19:56.920 回答
1

这允许我将空格传递给 cmd。数小时的研究一无所获;该线程不断出现,希望这对其他人有所帮助。

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

请注意,4 个双引号引导路径,这部分很重要。以5个引号开头的参数(/ C)不起作用,但尾随的5个可以分为4和1;结构如下:

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

如果您打开 cmd.exe 并发送一个命令,您只需要路径上的第一个引号(它不需要关闭),但 VB 需要尾随的引号来“关闭”引号。

祝你好运,伙计们。

于 2015-08-12T16:02:31.733 回答
0

简单点:

Process.Start("c:\Your exe file", """" & "string with space" & """")
于 2014-11-07T14:22:11.817 回答