"Overload resolution failed because no accessible 'New' accepts this number of arguments" 尝试将 stringbuilder 文本写入批处理文件时

"Overload resolution failed because no accessible 'New' accepts this number of arguments" when trying to write stringbuilder text into a batch file

我试图为我制作的需要制作和启动 .bat 文件的游戏重新创建启动器的源代码。 当需要将附加行打包到 .bat 中时,我发现了这个错误。 我什至彻底研究过。我问自己的原因是因为 none 我遇到的答案与我的情况相符。批处理设置变量,回显一些文本,然后启动游戏。这是代码,谢谢你帮助我。如果您需要,我会添加更多信息,我会尽力提供帮助。

    sb.AppendLine("@echo off")
    sb.AppendLine("set ttiUsername=" + username)
    sb.AppendLine("set ttiPassword=password")
    sb.AppendLine("set TTI_GAMESERVER=10.0.0.77")
    sb.AppendLine("set TTI_PORT=7198")
    sb.AppendLine("set /P PPYTHON_PATH=<PPYTHON_PATH")
    sb.AppendLine("echo ===============================")
    sb.AppendLine("echo Welcome to Toontown Rebuilt, %ttiUsername%!")
    sb.AppendLine("echo You are connecting to server %TTI_GAMESERVER%!")
    sb.AppendLine("echo The server port is %TTI_PORT%")
    sb.AppendLine("echo ===============================")
    sb.AppendLine("%PPYTHON_PATH% -m toontown.toonbase.ToontownStart")
    Dim File As New System.IO.StreamWriter
    File.WriteLine(sb.ToString())
    Process.Start("C:\Toontown Rebuilt Source\ToontownRebuilt\Launcher.bat")

System.IO.StreamWriter 构造函数需要一个参数。文件的名称或已创建的 Stream,其中连续的 Write 将转储字符串的内容。您缺少该参数。
但是这里还有其他问题需要修改

Using File = New System.IO.StreamWriter("C:\Toontown Rebuilt Source\ToontownRebuilt\Launcher.bat")
    File.WriteLine(sb.ToString())
End Using

Using 语句中的封装确保正确关闭和处理流

另一个有用的方法是 File.WriteAllText

 Dim file = "C:\Toontown Rebuilt Source\ToontownRebuilt\Launcher.bat"
 File.WriteAllText(file, sb.ToString())

您好,我使用了您的代码并修复了启动 bat 的错误。

这是我在 VB.NET 中的代码:

        Dim sb As New StringBuilder
        Dim username As String

        username = "test"

        sb.AppendLine("@echo off")
        sb.AppendLine("set ttiUsername=" + username)
        sb.AppendLine("set ttiPassword=password")
        sb.AppendLine("set TTI_GAMESERVER=10.0.0.77")
        sb.AppendLine("set TTI_PORT=7198")
        sb.AppendLine("set /P PPYTHON_PATH=<PPYTHON_PATH")
        sb.AppendLine("echo ===============================")
        sb.AppendLine("echo Welcome to Toontown Rebuilt, %ttiUsername%!")
        sb.AppendLine("echo You are connecting to server %TTI_GAMESERVER%!")
        sb.AppendLine("echo The server port is %TTI_PORT%")
        sb.AppendLine("echo ===============================")
        sb.AppendLine("%PPYTHON_PATH% -m toontown.toonbase.ToontownStart")
        Dim file As System.IO.StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter("C:\tmp\Launcher.bat", True)
        file.WriteLine(sb.ToString)
        file.Close()
        Process.Start("C:\tmp\Launcher.bat")