Visual Studio Write to a Text File

Visual Studio C# Write to a File:

In order to test this code, do the following:

Create a new Windows Application

Add a New form

Add a textbox to the form; use the default name for the textbox.

Make sure to change the Multiline Property of the textbox to True.

After changing the Multiline Property of the textbox to True, make the textbox larger in order to fill the form, but leave enough room at the top to add a button.

Add a button to the form above the textbox.

Rename the button, ‘pbSave’.

Paste the code below into the Click event of the ‘Save’ button.

Type some text into the text box, and click the ‘Save’ button.


 private void pbSave_Click(object sender, EventArgs e)

                System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Test\TextFile.txt");
            catch (System.Exception err)
                System.Windows.Forms.MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

Related post:
Visual Studio-Reading From A Text File 

Over 20 years programming experience 5 years Oracle SQL/Stored Procedure programming Over 10 years SQL 2000/2005/2008 Server SQL/Stored Procedure programming Over 18 years Visual Basic 3.0/4.0/5.0/6.0/.Net/2003/2005/2008/2010/2015 Over 10 years ASP.Net Over 7 years Visual C# Over 20 years Microsoft Office and VBA HTML JAVA COBOL EASYTRIEVE FORTRAN

Tagged with: , , , , ,
Posted in Visual Studio Code Examples

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: