在文本文件vb.net中用引号写多行

时间:2012-08-14 20:43:14

标签: vb.net string quotes writer

嗨有没有更容易的方法在文件中写多行,其中包含引号和其他类似的内容,或者是这样做的唯一方法

                Dim objwriter As New System.IO.StreamWriter(AppsDir & "EthIPChanger.bat")
            objwriter.WriteLine("@echo off")
            objwriter.WriteLine("netsh interface ip set address name=""" & "Local Area Connection""" & " static " & TB_EthIPAddress.Text & " " & TB_EthSubnetMask.Text & " " & TB_EthDefaultGateway.Text & " 1")
            objwriter.WriteLine("netsh interface ip set dns """ & "Local Area Connection""" & " static " & TB_EthDNS1.Text)
            objwriter.WriteLine("ipconfig /all > """ & AppsDir & "NetworkInfo.txt""")
            objwriter.WriteLine("echo hi > """ & AppsDir & "CheckLen.txt""")
            objwriter.Close()

我知道如果你使用python你可以做“”“然后在里面做任何事情并以”“”

结束它

vb.net中是否存在类似的内容?

谢谢

2 个答案:

答案 0 :(得分:2)

如果您使用objwriter.Write - 那么您可以自己提供vbcrlf - 然后您可以在一个write语句中放置多个'lines'。

例如:

Dim str2write As string
str2write  = "firstline" and Chr(34) & Chr(34) & vbcrlf
str2write &= Chr(34) & "second line" and Chr(34) & vbcrlf & vbcrlf
objwriter.write(str2write)
objwriter.close()

答案 1 :(得分:1)

您可以尝试使用StringBuilder:

    Dim objwriter As New System.IO.StreamWriter(AppsDir & "EthIPChanger.bat")
    Dim textToWrite As New System.Text.StringBuilder

    With textToWrite
        .Append("@echo off")
        .AppendFormat("netsh interface ip set address name={0}Local Area Connection{0} static {1} {2} {3} 1", Chr(34), TB_EthIPAddress.Text, TB_EthSubnetMask.Text, TB_EthDefaultGateway.Text)
        .AppendFormat("netsh interface ip set dns {0}Local Area Connection{0} static {1}", Chr(34), TB_EthDNS1.Text)
        .AppendFormat("ipconfig /all > {0}{1}{2}{0}", Chr(34), AppsDir, TB_EthDNS1.Text)
        .AppendFormat("echo hi > {0}{1}{2}{0}", Chr(34), AppsDir, CheckLen.Text)
    End With

    objwriter.WriteLine(textToWrite.ToString)
    objwriter.Close()
相关问题