你好我有一个叫做StreamWriter的gameWriter。我可以使用字符串构建器来格式化我的Stream Writer吗?我希望结果看起来像这样:playerOneName和playerTwoName是需要编写的变量。有人可以给我看一个示例代码来显示以下内容:
Output:
playerOnename, playerTwoname, _, _, _, _, _, _, _, _, _,
private void WriteData(string playerOneName, string playerTwoName)
{
StreamWriter gameStateWriter = null;
try
{
gameStateWriter = new StreamWriter(filepath, true);
gameStateWriter.WriteLine(playerOneName + " , " + playerTwoName);
}
catch (Exception ex)
{
txtOutcome.Text = "The following problem ocurred when writing to the file:\n"
+ ex.Message;
}
finally
{
if (gameStateWriter != null)
gameStateWriter.Close();
}
}
是的,这些是下划线,他们假设代表空间已准备好写入以保存一些文本。
我正在使用Visual Studio '08运行ASP.NET网站和表单。
答案 0 :(得分:2)
如何使用字符串生成器的流编写器,虽然我建议使用循环附加到构建器。
StringBuilder builder = new StringBuilder();
builder.Append(playerOneName)
builder.Append(",")
builder.Append(playerTwoName)
builder.Append(",")
StreamWriter nameWriter = new StreamWriter(filepath, true);
nameWriter.WriteLine(builder.ToString());
您只需将所有变量存储在数组
中string[] names = {playerOneName, playerTwoName, playerThreName};
foreach(string item in names)
{
builder.Append(item)
builder.Append(",")
}