0

嗨有没有更简单的方法可以在一个文件中写入多行,其中包含引号和其他类似的东西,或者是这样做的唯一方法

                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中是否存在类似的东西?

谢谢

4

2 回答 2

2

如果您使用 objwriter.Write - 那么您可以自己提供 vbcrlf - 然后您可以在一个写入语句中放置多个“行”。

例如:

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()
于 2012-08-14T21:31:03.920 回答
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()
于 2012-08-14T21:29:56.553 回答