banner



Writing To A File C#

Save Text File C# Example

Ecco Suprastyo

You can read see FULL SECTION this article in this http://camellabs.com/save-text-file-csharp-example/ or Visit my site at camellabs.com.

This article about Save Text File C# Example using System.IO.File,These examples sh o w 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. Example shows how to add 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 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 to make program how to write text file C# windows form, follow the steps to make it:

  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.

2. Then will appear the window New Project like the look below

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.

4. Create a new windows form like the below

5. Next step, Back to windows form and view 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();
}
}

}

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

7. Fill the textbox then click button "Save File", so the result will save file to your directory application path like the below:

We have explained how to make a simple program with save text file dialog C# using visual studio 2010 and mysql, for those of you who want to download the source code of the program also can. Hopefully this discussion is helpful to you.

You can see Save Text File C# Example from Github project in Here.

Thank you for reading this article about Save Text File C# Example, I hope this article is useful for you. Visit My Github about .Net Csharp in Here

Writing To A File C#

Source: https://medium.com/@ekosuprastyo15/save-text-file-c-example-3cbe4d0e3200

Posted by: huntyournothed.blogspot.com

0 Response to "Writing To A File C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel