关于vb.net:如何在VB .NET中将包含“”的字符串附加到现有字符串?

How can I append a string that contains “” to an existing string in VB .NET?

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    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 =""),并将空引号替换为存储在文本框(tbpath.selectedtext)中的文件夹路径。 不幸的是,由于Powershell脚本需要在路径字符串周围加引号(以防万一,所以要留空格),所以我很难弄清楚该怎么做。

我应该在那里做什么? 我应该将想要的内容构建到新变量(也许是vbPath = tbpath.selectedtext)中并将其放入新行中吗? 是否有"最佳做法"?


加倍报价

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

s =

$ Folder ="

在你的代码中

1
2
3
    If curLine.Contains("$Folder =""""") Then

    End If

您是否要curLine.Contains()检查文字字符串$Folder=""? 如果是这样,则需要将整个字符串放在双引号中并转义内部的双引号。 双引号通过加倍转义。 另外,如果要插入变量,则需要将其与字符串文字串联起来,因此命令可能看起来像这样:

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