在VB字符串中转义双引号 [英] Escape double quote in VB string

查看:360
本文介绍了在VB字符串中转义双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码从VB6执行 schtasks 命令。执行它时,如果它们包含空格,则忽略文件夹。例如,C:\program files\test\test.exe将被转换为c:\program。如何解决这个问题?

  MyAppname = Chr(34)& App.Path& \& App.EXEName& .exe& Chr(34)
StrCommand =schtasks / create / sc ONLOGON / RL HIGHEST / tn myapp / tr& MyAppname
Shell StrCommand,vbHide

新增任务新增为c :\program而不是C:\program files\test\test.exe



提前谢谢

解决方案

在VB6或VBScript字符串中转义引号在理论上很简单,看到可怕的你用另一个双引号避免双引号。



一个例子:



c:\program files\my app\app.exe



如果我想要转义双引号,所以我可以将其传递给由Joe或VB6 Shell函数列出的shell执行函数,我会写:

  escapedString =c:\program files\my app\app.exe

这是如何工作的?第一个和最后一个引号包装字符串,让VB知道这是一个字符串。然后在字符串中字面上显示的每个引号都有另一个双引号添加在它的前面,以避免它。



当您尝试传递一个字符串时,它会变得疯狂多个引用部分。记住,你要传递的每个报价必须被转义。



如果我想把这两个引用的短语作为一个单独的字符串分隔开(这并不罕见) ):



c:\program files\my app\app.exec:\documents and settings\steve



我会输入:

  escapedQuoteHell =c:\program files\my app\app.exec:\documents and settings\steve

我已经用一些更多引用的VBScript帮助我的系统管理员。



它不漂亮,但这是它的工作原理。 >

I have used following piece of code to execute schtasks command from VB6. While executing it, ignores folder if they contains spaces. For example, "C:\program files\test\test.exe" will be converted to "c:\program ". How do I solve this issue?

MyAppname =  Chr(34) & App.Path & "\" & App.EXEName & ".exe" & Chr(34)
StrCommand = "schtasks /create /sc ONLOGON /RL HIGHEST  /tn myapp  /tr " & MyAppname  
Shell StrCommand, vbHide   

New task added as "c:\program" instead of "C:\program files\test\test.exe"

Thanks in advance.

解决方案

Escaping quotes in VB6 or VBScript strings is simple in theory although often frightening when viewed. You escape a double quote with another double quote.

An example:

"c:\program files\my app\app.exe"

If I want to escape the double quotes so I could pass this to the shell execute function listed by Joe or the VB6 Shell function I would write it:

escapedString = """c:\program files\my app\app.exe"""

How does this work? The first and last quotes wrap the string and let VB know this is a string. Then each quote that is displayed literally in the string has another double quote added in front of it to escape it.

It gets crazier when you are trying to pass a string with multiple quoted sections. Remember, every quote you want to pass has to be escaped.

If I want to pass these two quoted phrases as a single string separated by a space (which is not uncommon):

"c:\program files\my app\app.exe" "c:\documents and settings\steve"

I would enter this:

escapedQuoteHell = """c:\program files\my app\app.exe"" ""c:\documents and settings\steve"""

I've helped my sysadmins with some VBScripts that have had even more quotes.

It's not pretty, but that's how it works.

这篇关于在VB字符串中转义双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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