Save a Text File C# Example

Introduction 

 
This article about saving a text file C# example using a System.IO.File. These examples show various ways to write text to a file. The first two examples use static convenience methods on the System.IO.File class to write each element of any IEnumerable<string> and a string to a text file. This example shows you how to add a text to a file when you have to process each line individually as you write to the file. Examples overwrite all existing content in the file, but this example shows you how to append text to an existing file.
 
These examples all write string literals to files, but more likely you will want to use the Format method, which has many controls for writing different types of values right or left-justified in a field, with or without padding, and so on.
 
Let’s make a program to write a text file C# windows form. We'll follow the steps to make it:
 
Step 1
 
Create a new application project. In Visual Studio, on the menu click File> New > Project. For more details, see the following menu on the display.

Save Text File C# Example
 
Step 2
 
Then the New Project window will appear

Save Text File C# Example
 
Step 3
 
Write down the name of the project that will be created on a field Name. Specify the directory storage project by accessing the field Location. Next, give the name of the solution in the Solution Name. Then click OK.

Save Text File C# Example
 
Step 4
 
Create a new windows form like below:

Save Text File C# Example
 
Step 5
 
Next, go back to the Windows form and view the code to write the following program listing:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.IO;namespace Save_File_txt  
  10. {  
  11.     public partial class Form1 : Form  
  12.     {  
  13.         public static string dirParameter = AppDomain.CurrentDomain.BaseDirectory + @"\file.txt";  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }        private void button1_Click(object sender, EventArgs e)  
  18.         {  
  19.             SaveEvent();  
  20.         }        public void SaveEvent()  
  21.         {  
  22.             DialogResult result;  
  23.             result = MessageBox.Show("Do you want to save file?""Konfirmasi", MessageBoxButtons.YesNo, MessageBoxIcon.Question);            if (result == DialogResult.No)  
  24.             {  
  25.                 return;  
  26.             }  
  27.             if (result == DialogResult.Yes)  
  28.             {  
  29.                 try  
  30.                 {  
  31.                     if (textBox1.Text != null && textBox2.Text != null && textBox3.Text != null)  
  32.                     {  
  33.                         saveFile(textBox1.Text, textBox2.Text, textBox3.Text);  
  34.                     }  
  35.                 }  
  36.                 catch (Exception err)  
  37.                 {  
  38.                     MessageBox.Show(err.ToString());  
  39.                 }  
  40.             }  
  41.         }        public void saveFile(string name,string telephone, string address)  
  42.         {  
  43.             string Msg = name + ";" + telephone + ";" + address;  
  44.     
  45.             // Save File to .txt  
  46.             FileStream fParameter = new FileStream(dirParameter, FileMode.Create, FileAccess.Write);  
  47.             StreamWriter m_WriterParameter = new StreamWriter(fParameter);  
  48.             m_WriterParameter.BaseStream.Seek(0, SeekOrigin.End);  
  49.             m_WriterParameter.Write(Msg);  
  50.             m_WriterParameter.Flush();  
  51.             m_WriterParameter.Close();  
  52.         }  
  53.     }  

Step 6
 
After you write down the program listings, press the F5 key to run the program and if you successfully, the result is:

Save Text File C# Example
 
Step 7
 
Fill the textbox then click button “Save File”, so the result will save the file to your directory application path like shown below:

Save Text File C# Example
 
We have explained how to make a simple program to save a text file dialog in C# using Visual Studio 2010 and MySQL. For those of you who want to download the source code of the program, you also can. Hopefully, this discussion was helpful to you.
 
You can see Save Text File C# Example from the Github project here.
 
Thank you for reading this article about saving a text file with C# Example, I hope this article was useful for you. Visit My Github about .Net Csharp here.