Write to TXT text from Textbox Control Code
// Write from testbox to TXT text
Protected Void Button5_click ( Object Sender, eventargs E)
{
String Text = Txtcontent. text;
If ( ! String . Isnullorempty (text ))
{
// Specify the full path of the file
String Filename = Server. mappath ( " ~ /TXT/test.txt " );
// Determine whether the file exists
If (File. exists (filename ))
{
//If yes, delete it first.
File. Delete (filename );
}
Else
{
// Create a file operation stream
Filestream stream = New Filestream (filename, filemode. Create );
// Create a write operation stream
Streamwriter = New Streamwriter (stream, encoding. utf8 );
// Write operation
Writer. Write (text );
// Clear the text in the control
Txtcontent. Text = String . Empty;
// Close the stream. Otherwise, an exception occurs.
Writer. Close ();
Stream. Close ();
}
}
Else
{
Response. Write ("<SCRIPT> alert (\"Empty column! \") </SCRIPT>");
}
}
Then read the data from the generated test.txt and display it in the Textbox Control (same method) Code
// Read text to textbox display
Protected Void Button6_click ( Object Sender, eventargs E)
{
String Filename = Server. mappath ( " ~ /TXT/test.txt " );
If (File. exists (filename ))
{
Filestream stream = New Filestream (filename, filemode. Open );
Streamreader = New Streamreader (stream, encoding. utf8 );
Txtcontent. Text = Reader. readtoend ();
Reader. Close ();
Stream. Close ();
}
Else
{
Response. Write ("<SCRIPT> alert (\"No test.txt file exists! \") </SCRIPT>");
}
}