gpt4 book ai didi

c# - String Writer 写入文本文件

转载 作者:太空宇宙 更新时间:2023-11-03 18:34:33 25 4
gpt4 key购买 nike

我有 100 个文本框,我试图将这些文本框中的所有文本写入一个文本文件,这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i <= 9; i++)
{
for (int j = 0; j <= 9; j++)
{
TextBox tb = new TextBox();
tb.MaxLength = (1);
tb.Width = Unit.Pixel(40);
tb.Height = Unit.Pixel(40);
// giving each textbox a different id 00-99
tb.ID = i.ToString() + j.ToString();
Panel1.Controls.Add(tb);
}
Literal lc = new Literal();
lc.Text = "<br />";
Panel1.Controls.Add(lc);
}
}

protected void btnShow_Click(object sender, EventArgs e)
{
StringWriter stringWriter = new StringWriter();
foreach (Control control in Panel1.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
if (string.IsNullOrEmpty(textBox.Text))
{
textBox.Style["visibility"] = "hidden";
}
// Write text to textfile.
stringWriter.Write("test.txt", textBox.Text+",");
} // end of if loop
}
}

我在 dev 文件夹中创建了一个名为 test.txt 的文件(我想它应该在该位置)它没有任何错误,但文本文件中没有任何文本。这是正确的方法吗?因为当我尝试调试时,stringWriter 的值将在第一个循环中以 test.txt 开头,在第二个循环中以 test.txttest.txt 开头。

最佳答案

你最好使用 StreamWriter 类:

StreamWriter sw = new StreamWriter("test.txt");  // You should include System.IO;

var textBox = control as TextBox;
if (textBox != null)
{
if (string.IsNullOrEmpty(textBox.Text))
{
textBox.Style["visibility"] = "hidden";
}
}
sw.Write(textBox.Text + ",");

关于c# - String Writer 写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17290552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com