2011-01-29 355 views
29

我已經使用以下一段代碼從VB6執行schtasks命令。執行它時,如果它們包含空格,則忽略文件夾。例如,"C:\program files\test\test.exe"將被轉換爲"c:\program "。我該如何解決這個問題?在VB字符串中轉義雙引號

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"

在此先感謝。

+0

只是好奇:你爲什麼使用VB6?到目前爲止,這種語言到處都沒有被取代? – Olhovsky 2011-01-29 09:05:23

+4

@Kdoto:這就是微軟希望你相信的東西。首先,他們大力投入妖魔化語言,然後與未洗的羣衆談話。 – wqw 2011-01-29 09:35:51

+3

@kdoto @wqw微軟[09年9月說](http://channel9.msdn.com/posts/funkyonex/What-is-Microsofts-Visual-Basic-6-Support-Strategy)還有幾百萬*使用VB6的人,所以也許並不奇怪。許多人有很多「傳統」的VB6代碼,微軟並沒有提供一個體面的升級路線。 – MarkJ 2011-01-29 14:16:44

回答

56

VB6或VBScript字符串中的轉義引號在理論上很簡單,雖然在查看時經常可怕。你用另一個雙引號轉義雙引號。

一個例子:

「C:\ Program Files文件\我的程序\ APP.EXE」

如果我想逃避雙引號,所以我可以把它傳遞到shell執行由喬上市功能或VB6 Shell函數我會寫它:

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

這是如何工作的?第一個和最後一個引號包裝字符串,讓VB知道這是一個字符串。然後,在字符串中逐字顯示的每個引號都會在其前面添加另一個雙引號以便將其轉義。

當您嘗試傳遞帶有多個引用段的字符串時,它變得瘋狂。請記住,你想要通過的每一個報價都必須逃脫。

如果我想通過這兩個引述短語用空格隔開一個字符串(這是並不少見):

「C:\ Program Files文件\我的程序\ APP.EXE」「C:\文件和設置\史蒂夫」

我會輸入:

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

我幫助我的系統管理員與一些VB腳本已經有更多的報價。

這並不美觀,但它是如何工作的。

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 
5

又如:

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

祝你好運!