如何将包含“”的字符串附加到VB .NET中的现有字符串?

时间:2013-05-20 16:43:08

标签: vb.net powershell psobject

我正在开发一个实用程序,它将获取.ps1脚本(Powershell)的内容,并在VB.NET Winform的上下文中运行它。基本上它是通过引用一些Powershell DLL,打开.ps1文件,读取一行文本并将其输入到将在按钮单击事件上运行的内部字符串来实现的。

这是我到目前为止的完整代码:

    Private Function RunScript(ByVal scriptText As String) As String

    ' create Powershell runspace 
    Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()

    ' open it 
    MyRunSpace.Open()

    ' create a pipeline and feed it the script text 
    Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()

    MyPipeline.Commands.AddScript(scriptText)

    ' add an extra command to transform the script output objects into nicely formatted strings 
    ' remove this line to get the actual objects that the script returns. For example, the script 
    ' "Get-Process" returns a collection of System.Diagnostics.Process instances. 
    MyPipeline.Commands.Add("Out-String")

    ' execute the script 
    Dim results As Collection(Of PSObject) = MyPipeline.Invoke()

    ' close the runspace 
    MyRunSpace.Close()

    ' convert the script result into a single string 
    Dim MyStringBuilder As New StringBuilder()

    For Each obj As PSObject In results
        MyStringBuilder.AppendLine(obj.ToString())
    Next

    ' return the results of the script that has 
    ' now been converted to text 
    Return MyStringBuilder.ToString()

End Function
' helper method that takes your script path, loads up the script 
' into a variable, and passes the variable to the RunScript method 
' that will then execute the contents 
Private Function LoadScript(ByVal filename As String) As String

    Try

        ' Create an instance of StreamReader to read from our file. 
        ' The using statement also closes the StreamReader. 
        Dim sr As New StreamReader(filename)

        ' use a string builder to get all our lines from the file 
        Dim fileContents As New StringBuilder()

        ' string to hold the current line 
        Dim curLine As String = ""

        ' loop through our file and read each line into our 
        ' stringbuilder as we go along 
        Do
            ' read each line and MAKE SURE YOU ADD BACK THE 
            ' LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF 
            curLine = sr.ReadLine()
            If curLine.Contains("") Then
                fileContents.AppendLine("$Folder=tbpath.selectedpath")
            Else
                fileContents.Append(curLine + vbCrLf)
            If curLine.Contains ($Folder="") Then
        Loop Until curLine Is Nothing

        ' close our reader now that we are done 
        sr.Close()

        ' call RunScript and pass in our file contents 
        ' converted to a string 
        Return fileContents.ToString()

对于冗长的东西感到抱歉,但我感到好奇的是curLine.Contains部分。我尝试做的是让解析器检测该行是否是特定行(读取$Folder = "")并用文本框中存储的文件夹路径替换空引号({ {1}})。不幸的是,由于Powershell脚本需要在路径字符串周围引用(如果有空格),我无法弄清楚如何做我想要它做的事情。

我该怎么办?我应该将我想要的内容构建到一个新变量(可能是tbpath.selectedtext)并将其放入新行吗?是否有任何"最佳做法"?

2 个答案:

答案 0 :(得分:3)

加倍引号

    Dim s As String = "$Folder = """""

s =

$ Folder =“”

在您的代码中

    If curLine.Contains("$Folder = """"") Then

    End If

答案 1 :(得分:1)

您是否希望curLine.Contains()检查文字字符串$Folder=""?如果是这样,你需要将整个字符串放在双引号中并转义内部字符串。双引号通过加倍来逃避。此外,如果要插入变量,则需要将其与字符串文字连接,因此命令应该如下所示:

If curLine.Contains ("$Folder = """"") Then
  fileContents.Append("$Folder = """ & tbpath.selectedpath & """")
End If