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

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

问题描述

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 est est.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 est est.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 filesmy appapp.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 filesmy appapp.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 filesmy appapp.exe" "c:documents and settingssteve"

I would enter this:

escapedQuoteHell = """c:program filesmy appapp.exe"" ""c:documents and settingssteve"""

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天全站免登陆