简体   繁体   English

在VB字符串中转义双引号

[英]Escape double quote in VB string

I have used following piece of code to execute schtasks command from VB6. 我使用以下代码来从VB6执行schtasks命令。 While executing it, ignores folder if they contains spaces. 执行时,如果文件夹包含空格,则忽略该文件夹。 For example, "C:\\program files\\test\\test.exe" will be converted to "c:\\program " . 例如, "C:\\program files\\test\\test.exe"将转换为"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" 新任务添加为"c:\\program"而不是"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. 在VB6或VBScript字符串中转义引号在理论上很简单,但在查看时经常会令人恐惧。 You escape a double quote with another double quote. 你用另一个双引号来逃避双引号。

An example: 一个例子:

"c:\\program files\\my app\\app.exe" “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: 如果我想要转义双引号,那么我可以将它传递给Joe列出的shell执行函数或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. 第一个和最后一个引号包装字符串,让VB知道这是一个字符串。 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" “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. 我帮助我的系统管理员使用了一些甚至更多引用的VBScripts。

It's not pretty, but that's how it works. 它不漂亮,但它是如何工作的。

Another example: 另一个例子:

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

Good luck! 祝好运!

Did you try using double-quotes? 你尝试过双引号吗? Regardless, no one in 2011 should be limited by the native VB6 shell command. 无论如何,2011年没有人应该受到原生VB6 shell命令的限制。 Here's a function that uses ShellExecuteEx, much more versatile. 这是一个使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM