vb.net如何将带空格的字符串传递到命令行 [英] vb.net How to pass a string with spaces to the command line
本文介绍了vb.net如何将带空格的字符串传递到命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Process调用外部程序:
I am trying to call an external program using 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个引号,例如:
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
推荐答案
工作原理:
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 + """
分为几部分:
cmd1 =
"
netsh advfirewall firewall add rule name =
""" + current_rulename + """
dir=in action=block
program=
""" + current_path + """
"
此代码连接两个单独的命令,这些命令使用带空格的STRINGS。
This code joins two separate commands that use STRINGS with spaces.
这篇关于vb.net如何将带空格的字符串传递到命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文