【问题标题】:String Writer writing into text file写入文本文件的字符串编写器
【发布时间】:2013-06-25 06:29:14
【问题描述】:

我有 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 开头。

【问题讨论】:

  • 这里定义的文件在哪里
  • 对不起,我不知道你的意思
  • 您的输出文件路径未定义
  • 输出文件?我只是将它写入我创建的 test.txt 文件中?
  • 问题是文本文件必须在 web dev 文件夹中,我不能把它放在其他地方吗?因为如果我把它放在别处,它就无法检测到文本文件。

标签: c# text-files stringwriter


【解决方案1】:

当您保持 StringWriter 打开时,数据不会保存到文件中。 在 btnShow_Click 结束时关闭它:

StringWriter.Close();

using (StringWriter stringwriter=new StringWriter())
{

//here is your code....
}

【讨论】:

    【解决方案2】:

    最好使用 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 + ","); 
    

    【讨论】:

      【解决方案3】:

      问题在于提交代码

       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              
          }
      

      参考MSDNMSDN

      【讨论】:

      • 您好,感谢您的回复,文本文件仍然是空的。我不明白 streamwriter 是如何工作的
      【解决方案4】:

      尝试在退出按钮单击事件之前使用以下方法刷新流:

      stringWriter.Flush();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-06
        • 1970-01-01
        • 1970-01-01
        • 2011-10-24
        相关资源
        最近更新 更多