string 在VB字符串中转义双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4835691/
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-09 00:58:11  来源:igfitidea点击:

Escape double quote in VB string

stringvb6escaping

提问by Suriyan Suresh

I have used following piece of code to execute schtaskscommand 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?

我使用以下代码schtasks从 VB6执行命令。执行它时,如果文件夹包含空格,则忽略文件夹。例如,"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   

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

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

Thanks in advance.

提前致谢。

回答by Steve Massing

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.

在 VB6 或 VBScript 字符串中转义引号在理论上很简单,但在查看时通常会令人恐惧。你用另一个双引号转义一个双引号。

An example:

一个例子:

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

“c:\程序文件\我的应用程序\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:

如果我想转义双引号以便我可以将它传递给 Joe 列出的 shell execute 函数或 VB6 Shell 函数,我会写它:

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.

这是如何运作的?第一个和最后一个引号将字符串包裹起来,并让 VB 知道这是一个字符串。然后在字符串中逐字显示的每个引号在其前面添加了另一个双引号以对其进行转义。

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"

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

我已经帮助我的系统管理员使用了一些带有更多引号的 VBScript。

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

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

回答by Brian Sweeney

Another example:

另一个例子:

Dim myPath As String = """" & Path.Combine(part1, part2) & """"

Good luck!

祝你好运!

回答by Joe Jordan

Did you try using double-quotes? Regardless, no one in 2011 should be limited by the native VB6 shell command. Here's a function that uses ShellExecuteEx, much more versatile.

您是否尝试使用双引号?无论如何,2011 年的任何人都不应受到本机 VB6 shell 命令的限制。这是一个使用 ShellExecuteEx 的函数,用途更广。

Option Explicit

Private Const SEE_MASK_DEFAULT = &H0

Public Enum EShellShowConstants
        essSW_HIDE = 0
        essSW_SHOWNORMAL = 1
        essSW_SHOWMINIMIZED = 2
        essSW_MAXIMIZE = 3
        essSW_SHOWMAXIMIZED = 3
        essSW_SHOWNOACTIVATE = 4
        essSW_SHOW = 5
        essSW_MINIMIZE = 6
        essSW_SHOWMINNOACTIVE = 7
        essSW_SHOWNA = 8
        essSW_RESTORE = 9
        essSW_SHOWDEFAULT = 10
End Enum

Private Type SHELLEXECUTEINFO
        cbSize        As Long
        fMask         As Long
        hwnd          As Long
        lpVerb        As String
        lpFile        As String
        lpParameters  As String
        lpDirectory   As String
        nShow         As Long
        hInstApp      As Long
        lpIDList      As Long     'Optional
        lpClass       As String   'Optional
        hkeyClass     As Long     'Optional
        dwHotKey      As Long     'Optional
        hIcon         As Long     'Optional
        hProcess      As Long     'Optional
End Type

Private Declare Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (lpSEI As SHELLEXECUTEINFO) As Long

Public Function ExecuteProcess(ByVal FilePath As String, ByVal hWndOwner As Long, ShellShowType As EShellShowConstants, Optional EXEParameters As String = "", Optional LaunchElevated As Boolean = False) As Boolean
    Dim SEI As SHELLEXECUTEINFO

    On Error GoTo Err

    'Fill the SEI structure
    With SEI
        .cbSize = Len(SEI)                  ' Bytes of the structure
        .fMask = SEE_MASK_DEFAULT           ' Check MSDN for more info on Mask
        .lpFile = FilePath                  ' Program Path
        .nShow = ShellShowType              ' How the program will be displayed
        .lpDirectory = PathGetFolder(FilePath)
        .lpParameters = EXEParameters       ' Each parameter must be separated by space. If the lpFile member specifies a document file, lpParameters should be NULL.
        .hwnd = hWndOwner                   ' Owner window handle

        ' Determine launch type (would recommend checking for Vista or greater here also)
        If LaunchElevated = True Then ' And m_OpSys.IsVistaOrGreater = True
            .lpVerb = "runas"
        Else
            .lpVerb = "Open"
        End If
    End With

     ExecuteProcess = ShellExecuteEx(SEI)   ' Execute the program, return success or failure

    Exit Function
Err:
    ' TODO: Log Error
    ExecuteProcess = False
End Function

Private Function PathGetFolder(psPath As String) As String
    On Error Resume Next
    Dim lPos As Long
    lPos = InStrRev(psPath, "\")
    PathGetFolder = Left$(psPath, lPos - 1)
End Function