write in a txt file c# code example
Example 1: write text files with C#
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);
...
string readText = File.ReadAllText(path);
Example 2: save text input into a txt file in c#
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 WindowsFormsApplication2
{
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TextWriter txt = new StreamWriter("C:\\demo\\demo.txt");
txt.Write(textBox1.Text);
txt.Close();
}
}
}