Greg Owen
Over 27 Years of Programming Experience
Programming Examples, Portfolio, and More
Home

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)
      {

      try
      {
      System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Test\TextFile.txt file.WriteLine(textBox1.Text);
      file.Close();
      }
      catch (System.Exception err)
      {
      System.Windows.Forms.MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
      }      
      
    
#CSharp #VisualStudio
Some words from the sponsors...

Add a C# Dropdown to a Bootstrap Modal Dialog

This example combines C# and Bootstrap to create a bootstrap modal dialog containing an asp dropdown and an asp button to retrieve...

Continue Reading

Use C# to Populate Dropdown Lists With XML

The code below opens an xml file, reads the data from the xml file, dynamically adds a label for the dropdown list, and adds the dropdown list to the asp.net page...

Continue Reading

Add Style Properties to ASP.Net Controls to Improve Appearance

The default formatting for ASP.Net controls is very dated.  The default formatting is basically the same as the formatting in Visual Basic 1.0 ...

Continue Reading

Export To A Text File From A DataGridView Control

Add a button control to Form1.
Add a DataGridView Control to Form1....

Continue Reading