簡體   English   中英

在VB字符串中轉義雙引號

[英]Escape double quote in VB string

我使用以下代碼來從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列出的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。

它不漂亮,但它是如何工作的。

另一個例子:

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

祝好運!

你嘗試過雙引號嗎? 無論如何,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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM