vb.net 如何将带空格的字符串传递给命令行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13097872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 10:54:56  来源:igfitidea点击:

vb.net How to pass a string with spaces to the command line

vb.netprocesscommand-line-argumentsspace

提问by Chiwda

I am trying to call an external program using Process:

我正在尝试使用 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()

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)。如果它有空格,命令会阻塞 - 没有空格,它工作正常。我尝试添加 1、2 或 3 个引号,如下所示:

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

and also

并且

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

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

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

回答by andy

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

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

Word command-line-arguments space issues

Word 命令行参数空间问题

回答by Mark Hurd

Windowsdoes 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.

Windows不提供将带空格的参数作为单个参数保存的常用方法。但是,您已经尝试了许多相对通用的标准。

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

因此,它归结为确定使用什么参数处理,mktorrent.exe或者在您尝试传递文件名时,使用“MSDOS”8.3 格式的路径作为没有空格的路径。

For the latter, this answerpoints to the Win32API GetShortPathName.

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

Of course, 8.3 filenames canbe 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 mktorrentsupplies.

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

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

由于您的评论暗示没有传递引号,因此我确认我'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)

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 wscriptis seeing the quotes and is accumulating three arguments for the script.

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

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

顺便说一句,我刚刚注意到您的示例尝试在文件名周围获取引号修改fn变量。您是否.WorkingDirectory使用应使用未修改文件名的行来满足此要求?

回答by KranDes

This allows me to pass spaces to cmd. Hours of research turned up nothing; this thread came up constantly, hopefully this will help someone else.

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

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

note that the 4 double quotes lead the path, this part is important. leading the argument (/C) with 5 quotes doesn't work, but the trailing five can be divided into 4 and 1; and structured as such:

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

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

If you open cmd.exe and just send a command, you just need the first quote on the path (it doesn't need to be closed) but VB needs the trailing ones to "close" the quotes out.

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

best of luck, guys.

祝你好运,伙计们。

回答by David BS

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

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

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

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

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

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

Just it!

只是它!

回答by GusNV

This WORKS:

这有效:

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)

Basically, the variables with spaces need to be enclosed like this:

基本上,带空格的变量需要像这样封闭:

""" + string_with_spaces + """

Broken into parts:

分成几部分:

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

This code joins two separate commands that use STRINGS with spaces.

此代码将两个使用带空格的 STRINGS 的单独命令连接起来。

回答by jcq

Be simple:

简单点:

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