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.
Step 2
Then the New Project window will appear
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.
Step 4
Create a new windows form like below:
Step 5
Next, go back to the Windows form and view the code to write the following program listing:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace Save_File_txt
{
public partial class Form1 : Form
{
public static string dirParameter = AppDomain.CurrentDomain.BaseDirectory + @"\file.txt";
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
SaveEvent();
} public void SaveEvent()
{
DialogResult result;
result = MessageBox.Show("Do you want to save file?", "Konfirmasi", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.No)
{
return;
}
if (result == DialogResult.Yes)
{
try
{
if (textBox1.Text != null && textBox2.Text != null && textBox3.Text != null)
{
saveFile(textBox1.Text, textBox2.Text, textBox3.Text);
}
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
} public void saveFile(string name,string telephone, string address)
{
string Msg = name + ";" + telephone + ";" + address;
// Save File to .txt
FileStream fParameter = new FileStream(dirParameter, FileMode.Create, FileAccess.Write);
StreamWriter m_WriterParameter = new StreamWriter(fParameter);
m_WriterParameter.BaseStream.Seek(0, SeekOrigin.End);
m_WriterParameter.Write(Msg);
m_WriterParameter.Flush();
m_WriterParameter.Close();
}
}
}
Step 6
After you write down the program listings, press the F5 key to run the program and if you successfully, the result is:
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:
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.