C# 字符串写入器写入文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17290552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String Writer writing into text file
提问by user2376998
I have 100 textboxes , and i am trying to get all the text from these textboxes to be written into a textfile , this is my code :
我有 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
}
}
I have created a file name call test.txt at the dev folder ( I suppose its where it suppose to be) it doesn't have any error , but the text file doesn't have any text in it . Is this the correct way to do it? Because when I tried to debug , the value of stringWriter will begin with test.txt in the first loop and test.txttest.txt in the second loop.
我在 dev 文件夹中创建了一个名为 test.txt 的文件名(我想它应该在那里)它没有任何错误,但文本文件中没有任何文本。这是正确的方法吗?因为当我尝试调试时,stringWriter 的值将在第一个循环中以 test.txt 开头,在第二个循环中以 test.txttest.txt 开头。
回答by MDMalik
The issue is in Submit Code
问题出在提交代码中
protected void btnShow_Click(object sender, EventArgs e)
{
System.IO.StreamWriter stringWriter= new System.IO.StreamWriter("c:\test.txt");
foreach (Control control in Panel1.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
if (string.IsNullOrEmpty(textBox.Text))
{
textBox.Style["visibility"] = "hidden";
}
stringWriter.Write( textBox.Text+","); // Write text to textfile.
} // end of if loop
}
回答by r.mirzojonov
It would be better for you to use StreamWriter class:
最好使用 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 + ",");
回答by MrClan
Try flushing out the stream just before exiting your button click event using:
尝试在退出按钮单击事件之前使用以下方法刷新流:
stringWriter.Flush();
回答by ako
the data will not save to the file when you keep the StringWriteropen. at the end of btnShow_ClickClose it:
当您保持StringWriter打开时,数据不会保存到文件中。在btnShow_Click结束时关闭它:
StringWriter.Close();
or
或者
using (StringWriter stringwriter=new StringWriter())
{
//here is your code....
}

