简体   繁体   中英

String Writer writing into text file

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 :

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.

It would be better for you to use StreamWriter class:

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 + ","); 

the data will not save to the file when you keep the StringWriter open. at the end of btnShow_Click Close it:

StringWriter.Close();

or

using (StringWriter stringwriter=new StringWriter())
{

//here is your code....
}

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              
    }

Reference MSDN and MSDN

在退出按钮点击事件之前,尝试使用以下方法清除流:

stringWriter.Flush();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM