0

我正在尝试简化一些代码行,试图在 string.format 方法中分配多行字符串,但它会在显示“预期表达式”的“</a>”字符处引发异常:

Dim RegistryScript As String = String.Format(<![CDATA[
    Windows Registry Editor Version 5.00
    [HKEY_CURRENT_USER\Environment]
    "PATH"="{0}"
    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
    "PATH"="{1}"
]]></a>.Value, String.Join(";", PATH_Current), String.Join(";", PATH_Local))

所以如果我不能这样做,那么也许我可以做一些类似的字符串技巧?

这是我想让它在代码中更具可读性的原始代码:

' Current user
IO.File.WriteAllText(FilePath, " Windows Registry Editor Version 5.00" & Environment.NewLine, System.Text.Encoding.Unicode)
IO.File.AppendAllText(FilePath, "[HKEY_CURRENT_USER\Environment]" & Environment.NewLine, System.Text.Encoding.Unicode)
IO.File.AppendAllText(FilePath, String.Format("""PATH""=""{0}""", String.Join(";", PATH_Current)) & Environment.NewLine, System.Text.Encoding.Unicode)

' Local Machine
IO.File.AppendAllText(FilePath, " Windows Registry Editor Version 5.00" & Environment.NewLine, System.Text.Encoding.Unicode)
IO.File.AppendAllText(FilePath, "[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]" & Environment.NewLine, System.Text.Encoding.Unicode)
IO.File.AppendAllText(FilePath, String.Format("""PATH""=""{0}""", String.Join(";", PATH_Local)) & Environment.NewLine, System.Text.Encoding.Unicode)

Console.WriteLine(" [+] Backup done!")

我也尝试过使用字符串生成器,但是我得到了一些不可读的代码,这就是为什么我尝试将它们全部放在多行字符串中的原因。

4

3 回答 3

1

你可以做:

Dim multi As String = <![CDATA[
    Some
    multiline text
]]>.Value

现在你可以这样做:

Console.WriteLine(String.Format("multi{0} string = {1}", "line", multi))
于 2013-09-07T20:38:12.317 回答
1

让自己的事情变得更轻松。

Using sr As New StreamReader({path})
   sr.WriteLine("whatever")
   sr.writeLine("somemore on next line")
End Using
于 2013-09-07T20:40:22.707 回答
1

VB.NET 不支持多行字符串,但您可以使用 XML 文字模拟它们。但是,您的文字必须是有效的 XML。<a>要么以标签开头,要么在</a>末尾省略结束标签。

于 2013-09-07T20:42:14.560 回答