在VB字符串中转义双引号

时间:2011-01-29 07:20:20

标签: string vb6 escaping

我已经使用以下代码从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"

提前致谢。

3 个答案:

答案 0 :(得分:65)

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

一个例子:

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

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

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

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

当您尝试传递带有多个引用部分的字符串时,它会变得更加疯狂。请记住,您要传递的每个报价都必须转义。

如果我想将这两个引用的短语作为由空格分隔的单个字符串传递(这并不罕见):

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

我会输入:

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

我帮助我的系统管理员使用了一些甚至更多引号的VBScripts。

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

答案 1 :(得分:6)

另一个例子:

Dim myPath As String = """" & Path.Combine(part1, part2) & """"
祝你好运!

答案 2 :(得分:4)

您尝试使用双引号吗?无论如何,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